package com.jwj.testback.recordsheet.service.impl;



import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwj.testback.backups.service.MysqlBackupsService;
import com.jwj.testback.recordsheet.dao.RecordsheetMapper;
import com.jwj.testback.recordsheet.pojo.FieldGet;
import com.jwj.testback.recordsheet.pojo.Recordsheet;
import com.jwj.testback.recordsheet.service.RecordsheetService;


import com.jwj.testback.util.TakeTimeUtil;
import com.jwj.testback.ws.JqWebSocket;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;


import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import com.jwj.testback.util.IP;

/**
 * <p>
 *  备份实现类 并记录
 * </p>
 *
 * @author JWJ
 * @since 2021-12-17
 */
@Service
public class RecordsheetServiceImpl extends ServiceImpl<RecordsheetMapper, Recordsheet> implements RecordsheetService {

    public static final String Ip = IP.Ip;

    //window
//    public static final String filePre = System.getProperty("user.dir")+"/testback/src/main/resources/static/backupsql/";
    //linux
    public static final String filePre = System.getProperty("user.dir")+"/src/main/resources/static/backupsql/";

    @Resource
    private MysqlBackupsService mysqlBackupsService;

    @Resource
    DataSourceTransactionManager dataSourceTransactionManager;

    @Autowired
    TransactionDefinition transactionDefinition;

    //库-》表-》年（月/日）   三个维度确定一个唯一表
    @SneakyThrows
    @Override
//    @Async("asyncServiceExecutor")
    public String add(String databaseName, String tableName,String fieldTime,String customField, Integer type,Integer endType,String customStart, String customEnd) {
        int wb= 0;
        int wt= 0;

        System.out.println("时间字段："+fieldTime+"----"+"自定义字段："+customField+"----"+"类型："+type+"---"+"自定义类型："+endType);

        String userName =IP.username;
        String passWord =IP.password;
//        String userName ="123";
//        String passWord ="421";

        String newdataName = "";
        String newtableName = null;
        String Name = "";

//        //获取当前路径
        String filePath = "";

        //备份文件名
        String mysqlFileName = "";

        Integer offset = 0;
        Integer pageSize = 3000;


        //查找源表主键
        String tableIdName = baseMapper.findIdName(databaseName, tableName);
        if (tableIdName==null){
            return "库名、表名错误，或表中无主键";
        }

        //查找时间字段是否存在
        if (fieldTime!=null) {
            Integer sureTimefield = baseMapper.findTimeField(databaseName, tableName, fieldTime);
            if (sureTimefield == 0) {
                return "表中无此时间字段";
            }
        }

        Recordsheet recordsheet = new Recordsheet();


        recordsheet.setDatabaseName(databaseName);
        recordsheet.setTableName(tableName);

        recordsheet.setType(type);
        recordsheet.setEndType(endType);
//        recordsheet.setSure(sure);
        recordsheet.setTableIdName(tableIdName);
        recordsheet.setCustomStart(customStart);
        recordsheet.setCustomEnd(customEnd);

        //有分类字段时
        if (customField!=null){
            Integer sureCustom = baseMapper.findCustomField(databaseName, tableName, customField);
            if (sureCustom==0){
                return "表中无此分类字段";
            }

            //不为空，并且是正确字段，时间与类型字段一起分
            if (customStart !=null && customEnd!=null){
                //==============================分类字段与自定义时间字段 ，按年月、、等
//            System.out.println("自定义时间不为空,结束："+ customEnd);
                if (endType==1){
//                System.out.println("自定义 年");
                    //年
//                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");//设置日期格式
                    SimpleDateFormat dbdate = new SimpleDateFormat("yyMMddHHmmss");//设置日期格式
//                    String nowName = df.format(new Date());
                    String dbstr = dbdate.format(new Date());

                    newdataName = databaseName + "_cs_"+customField+dbstr;

                    //路径
                    filePath = filePre + newdataName + "/";

                    //数据库存在，就是wb=1
                    wb = whetherBase(newdataName);
                    if (wb == 0) {
                        //数据库不存在，则先创建数据库
                        baseMapper.createLibrary(newdataName);

                        //随后插入数据库
                        //设置添加数据库的名字和URL
//                        datasource.setDsname(newdataName);
//                        datasource.setUrl(PRE + newdataName + Suffix);
//                        datasourceMapper.insert(datasource);
                    }

                    //设置添加新库
                        recordsheet.setNewdataName(newdataName);
                    recordsheet.setStatus(2);
                    recordsheet.setTime(new Date());
//                TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                    int insert = baseMapper.insert(recordsheet);

                    while (true) {
//                开启事务
                        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);

                        try {
                            Map<String, List<Long>> TbMap = new HashMap<>();
                            //数据库查询数据
                            List<Long> list = baseMapper.customData(databaseName,tableName,tableIdName,fieldTime,customStart,customEnd,offset,pageSize);
                            //判断是否有数据
                            boolean empty = CollectionUtils.isNotEmpty(list);

                            if (offset==0&&empty==false){
                                dataSourceTransactionManager.rollback(transactionStatus);
                                recordsheet.setStatus(0);
                                recordsheet.setEndtime(new Date());
                                String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());

                                recordsheet.setSpendtime(spendTime);
                                baseMapper.updateById(recordsheet);
                                // ws发送消息
                                JqWebSocket.sendMessage();
                                return "此时间段无数据";
                            }
                            offset += pageSize;
                            if (empty) {
                                //分页有值

                                //判断目标表是否存在
                                Integer finRes = 0;

                                // 一页一页的处理
                                for (int i = 0; i < list.size(); i++) {
                                    //2.判断哪一年的：
                                    SimpleDateFormat format = new SimpleDateFormat("yyyy");

                                    //得到当前id对应的时间字段 yyyy年份
                                    Long id = list.get(i);

                                    //得到数据库Date类型时间值 , 和自定义字段的值
//                                    Date date = baseMapper.findDate(fieldTime, databaseName, tableName, tableIdName, id);
//                                    String year = format.format(date);
                                    FieldGet fieldGet = baseMapper.fieldGet(fieldTime, customField, databaseName, tableName, tableIdName, id);
                                    Date date = fieldGet.getFieldTime();
                                    String year = format.format(date);

                                    int tinyInt = fieldGet.getCustomField();

                                    Name = newtableName; // 需要打印中间代理的Name

                                    //3.设置目标表名：格式、例：finance_record_tb2020_type1
                                    newtableName = tableName + year +"_"+customField+tinyInt;

                                    //4.判断目标表是否存在
                                    wt = baseMapper.whetherTable(newdataName, newtableName);
                                    if (wt == 0) {
                                        //表不存在，则先创建表
                                        baseMapper.addTable(newdataName, newtableName, databaseName, tableName);
                                    }

                                    boolean result = newtableName.equals(Name);
//                                System.out.println("前面是否等于后面："+result);
                                    if (result == false && Name != null) {
                                        //如果前面表不等于后面的， 先判断map里有没有这个表key
                                        boolean b = TbMap.containsKey(newtableName);
                                        //如果有
                                        if (b == true) {//添加id到这个表中

                                            TbMap.get(newtableName).add(id);
//                                        System.out.println("有这个key："+newtableName);
                                        } else {
                                            //如果没有，就new一个list放id 存map
//                                        List<Long> tableIdList= new ArrayList<>();
//                                        tableIdList.add(id);
                                            TbMap.put(newtableName, new ArrayList<>());
                                            TbMap.get(newtableName).add(id);
//                                        System.out.println("没有这个key，这里的name"+Name+"，这里的新表名："+newtableName);
                                        }
                                    } else {
                                        boolean c = TbMap.containsKey(newtableName);
//                                    System.out.println("是否有"+newtableName+"::"+c);
                                        if (c == false) {
                                            //如果表名相等,直接添加id
                                            TbMap.put(newtableName, new ArrayList<>());
                                            TbMap.get(newtableName).add(id);
                                        } else {
                                            TbMap.get(newtableName).add(id);
                                        }
                                    }
                                }
                                for (Map.Entry<String, List<Long>> entry : TbMap.entrySet()) {
                                    try {
                                        //5.放数据
                                        int result = testInsert(newdataName, entry.getKey(), databaseName, tableName, tableIdName, entry.getValue());
                                        finRes += result;
                                    } catch (RuntimeException e) {
                                        if (e instanceof DuplicateKeyException) {
                                            System.out.println("该接口数据主键冲突" + e.getMessage());
                                            //这页的第一个数据主键已存在，则跳过当前页循环，进行下一页数据插入
                                            break;
                                        } else {
                                            dataSourceTransactionManager.rollback(transactionStatus);
                                            System.out.println("其他错误" + e.getMessage());
                                            //异常错误，失败记录
                                            recordsheet.setStatus(0);
                                            recordsheet.setEndtime(new Date());
                                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                                            recordsheet.setSpendtime(spendTime);
                                            // 插入失败的记录
                                            baseMapper.updateById(recordsheet);
                                            // ws发送消息
                                            JqWebSocket.sendMessage();
                                            return "异常错误";
                                        }
                                    }
                                }
//                            ws发送消息
                                JqWebSocket.sendMessage();

                                System.out.println("list==" + empty);
                            } else {
                                //做最后一个表的备份
                                //最后分完做备份
                                List<String> TabNamels = baseMapper.ShowAllTB(newdataName);

                                for (int i = 0; i < TabNamels.size(); i++) {
                                    mysqlFileName = TabNamels.get(i) + ".sql";
                                    baseMapper.rename(tableName,TabNamels.get(i),newdataName);

                                    mysqlBackupsService.mysqlBackups(Ip,filePath, newdataName, tableName, mysqlFileName, userName, passWord);
                                    //做删除操作
                                    baseMapper.delTB(newdataName,tableName);
                                    System.out.println("备份完毕,删除一个已备份的表");
                                }

                                //判断数据库中是否还有表，没有，则删除库，有则不做处理
                                List<String> allTb = baseMapper.ShowAllTB(newdataName);
                                if (allTb.isEmpty()){
                                    //判断数据库是否有表，如果为空，就删除该数据库
                                    baseMapper.delDataBase(newdataName);
                                }

                                //集合里没有数据了,到最后了，状态为：1.成功
                                //0.不删除，1.删除
//                                if (sure == 0) {
//                                    //不删除
//                                    //添加成功记录
//                                    dataSourceTransactionManager.rollback(transactionStatus);
//                                    //结束，更改记录数据
//                                    recordsheet.setStatus(1);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                    recordsheet.setSpendtime(spendTime);
//                                    baseMapper.updateById(recordsheet);
////                                ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    return "成功，不删除原始数据";
//                                } else {
//                                    //自定义时间的删除
//                                    try {
//                                        List<Long> allIdList = baseMapper.allCustomId(tableIdName, databaseName, tableName, fieldTime, customStart, customEnd);
//
//                                        int i = delCustomSure(databaseName, tableName, tableIdName, allIdList);
//
//                                        if (i == 0) {
//                                            //没有删除数据
//                                            dataSourceTransactionManager.rollback(transactionStatus);
//                                            //删除数据失败，失败记录
//                                            recordsheet.setStatus(0);
//                                            recordsheet.setEndtime(new Date());
//                                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//
//                                            recordsheet.setSpendtime(spendTime);
//                                            baseMapper.updateById(recordsheet);
//                                            //                                ws发送消息
//                                            JqWebSocket.sendMessage();
//                                            return "删除失败";
//                                        }
//
//                                        recordsheet.setStatus(1);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                        recordsheet.setSpendtime(spendTime);
//                                        baseMapper.updateById(recordsheet);
//                                        dataSourceTransactionManager.commit(transactionStatus);
//                                        //  ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "成功添加表并删除了原始数据";
//                                    } catch (Exception n) {
//                                        System.out.println("n的报错" + n.getMessage());
//                                        dataSourceTransactionManager.rollback(transactionStatus);
//                                        //异常错误，失败记录
//                                        recordsheet.setStatus(0);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//
//                                        recordsheet.setSpendtime(spendTime);
//                                        //插入失败的记录
//                                        baseMapper.updateById(recordsheet);
//                                        //                                ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "原始数据删除失败，异常错误";
//                                    }
//                                }
                            }
                            //提交事务
                            dataSourceTransactionManager.commit(transactionStatus);

                        } catch (Exception e1) {
                            dataSourceTransactionManager.rollback(transactionStatus);
                            //异常错误，失败记录
                            recordsheet.setStatus(0);
                            recordsheet.setEndtime(new Date());
                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                            recordsheet.setSpendtime(spendTime);
                            //插入失败的记录
                            baseMapper.updateById(recordsheet);
                            //失败后，判断数据库是否存在，存在则插入数据库，不存在则不插入
//                            if (wb == 0) {
//                                datasourceMapper.insert(datasource);
//                            }
                            //                                ws发送消息
                            JqWebSocket.sendMessage();
                            System.out.println("e1的报错" + e1.getMessage());
                            return "添加表失败，请检查填写是否错误";
                        }
                    }
                }else if (endType==2){
                    //月
//                System.out.println("自定义 月");

//                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");//设置日期格式
//                    String nowName = df.format(new Date());
                    SimpleDateFormat dbdate = new SimpleDateFormat("yyMMddHHmmss");//设置日期格式
                    String dbstr = dbdate.format(new Date());

                    newdataName = databaseName + "_cs_"+customField+dbstr;
                    //路径
                    filePath = filePre + newdataName + "/";

                    //判断数据库是否存在
                    wb = whetherBase(newdataName);

                    //数据库存在，就是wb=1
                    if (wb == 0) {
                        System.out.println("数据库不存在"+newdataName);
                        //数据库不存在，则先创建数据库
                        baseMapper.createLibrary(newdataName);
                        //随后插入数据库
                        //设置添加数据库的名字和URL

//                        int insert = datasourceMapper.insert(datasource);
//                        System.out.println("数据库插入状态：" + insert);
                    }

                    recordsheet.setNewdataName(newdataName);
                    recordsheet.setStatus(2);
                    recordsheet.setTime(new Date());
//                TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                    int insert = baseMapper.insert(recordsheet);
                    System.out.println("插入状态：" + insert);
                    while (true) {
//                开启事务
                        TransactionStatus MonthsStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                        try {
                            Map<String, List<Long>> TbMap = new HashMap<>();
                            //数据库查询数据
                            List<Long> list = baseMapper.customData(databaseName,tableName,tableIdName,fieldTime,customStart,customEnd,offset,pageSize);
                            //判断是否有数据
                            boolean empty = CollectionUtils.isNotEmpty(list);
//                        System.out.println("判断是否有数据："+empty);
                            if (offset==0&&empty==false){
                                dataSourceTransactionManager.rollback(MonthsStatus);
                                //删除数据失败，失败记录
                                recordsheet.setStatus(0);
                                recordsheet.setEndtime(new Date());
                                String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                                recordsheet.setSpendtime(spendTime);
                                baseMapper.updateById(recordsheet);
                                //ws发送消息
                                JqWebSocket.sendMessage();
                                return "此时间段无数据";
                            }
                            offset += pageSize;
                            if (empty) {
                                //分页有值
                                //判断目标表是否存在
                                Integer finRes = 0;
                                for (int i = 0; i < list.size(); i++) {
                                    //2.判断哪一年的：
                                    SimpleDateFormat format = new SimpleDateFormat("yyyy_MM");
                                    //得到当前id对应的时间字段 yyyy_MM
                                    Long id = list.get(i);
                                    //得到数据库Date类型时间值

//                                    Date date = baseMapper.findDate(fieldTime, databaseName, tableName, tableIdName, id);
//                                    String year_month = format.format(date);
                                    FieldGet fieldGet = baseMapper.fieldGet(fieldTime, customField, databaseName, tableName, tableIdName, id);
                                    Date date = fieldGet.getFieldTime();
                                    String year_month = format.format(date);

                                    int tinyInt = fieldGet.getCustomField();

                                    Name = newtableName; // 需要打印中间代理的Name

                                    //3.设置目标表名：格式、例：finance_record_tb2020_02_type1
                                    newtableName = tableName + year_month+"_"+customField+tinyInt;
                                    //4.判断目标表是否存在
                                    wt = baseMapper.whetherTable(newdataName, newtableName);
                                    if (wt == 0) {
                                        //表不存在，则先创建表
                                        baseMapper.addTable(newdataName, newtableName, databaseName, tableName);
                                    }
                                    boolean result = newtableName.equals(Name);
                                    if (result == false && Name != null) {
                                        //如果前面表不等于后面的， 先判断map里有没有这个表key
                                        boolean b = TbMap.containsKey(newtableName);
                                        //如果有
                                        if (b == true) {//添加id到这个表中
                                            TbMap.get(newtableName).add(id);
//                                        System.out.println("有这个key：" + newtableName);
                                        } else {
                                            //如果没有，就new一个list放id 存map
//                                        List<Long> tableIdList= new ArrayList<>();
//                                        tableIdList.add(id);
                                            TbMap.put(newtableName, new ArrayList<>());
                                            TbMap.get(newtableName).add(id);
//                                        System.out.println("没有这个key，这里的name" + Name + "，这里的新表名：" + newtableName);
                                        }
                                    } else {
                                        boolean c = TbMap.containsKey(newtableName);
//                                    System.out.println("是否有"+newtableName+"::"+c);
                                        if (c == false) {
                                            //如果表名相等,直接添加id
                                            TbMap.put(newtableName, new ArrayList<>());
                                            TbMap.get(newtableName).add(id);
                                        } else {
                                            TbMap.get(newtableName).add(id);
                                        }
                                    }
                                }
                                for (Map.Entry<String, List<Long>> entry : TbMap.entrySet()) {
                                    try {
                                        //5.放数据
                                        int result = testInsert(newdataName, entry.getKey(), databaseName, tableName, tableIdName, entry.getValue());
                                        finRes += result;
                                    } catch (RuntimeException e) {
                                        if (e instanceof DuplicateKeyException) {
                                            System.out.println("该接口数据主键冲突" + e.getMessage());
//                                    dataSourceTransactionManager.rollback(transactionStatus);
                                            //这页的第一个数据主键已存在，则跳过当前页循环，进行下一页数据插入
                                            break;
                                        } else {
                                            dataSourceTransactionManager.rollback(MonthsStatus);
                                            System.out.println("其他错误" + e.getMessage());
                                            //异常错误，失败记录
                                            recordsheet.setStatus(0);
                                            recordsheet.setEndtime(new Date());
                                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                                            recordsheet.setSpendtime(spendTime);
                                            //插入失败的记录
                                            baseMapper.updateById(recordsheet);
                                            //                            ws发送消息
                                            JqWebSocket.sendMessage();
                                            return "异常错误";
                                        }
                                    }
                                }
                                //                            ws发送消息
                                JqWebSocket.sendMessage();

//                            System.out.println("最终成功数：" + finRes);
                                System.out.println("list==" + empty);
                            } else {
                                //做最后一个表的备份
                                //最后分完做备份

                                List<String> TabNamels = baseMapper.ShowAllTB(newdataName);

                                for (int i = 0; i < TabNamels.size(); i++) {
                                    mysqlFileName = TabNamels.get(i) + ".sql";
                                    baseMapper.rename(tableName,TabNamels.get(i),newdataName);

                                    mysqlBackupsService.mysqlBackups(Ip,filePath, newdataName, tableName, mysqlFileName, userName, passWord);
                                    //做删除操作
                                    baseMapper.delTB(newdataName,tableName);
                                    System.out.println("备份完毕,删除一个已备份的表");
                                }

                                //判断数据库中是否还有表，没有，则删除库，有则不做处理
                                List<String> allTb = baseMapper.ShowAllTB(newdataName);
                                if (allTb.isEmpty()){
                                    //判断数据库是否有表，如果为空，就删除该数据库
                                    baseMapper.delDataBase(newdataName);
                                }

                                //集合里没有数据了,到最后了，状态为：1.成功
                                //分页无值
                                //0.不删除，1.删除
//                                if (sure == 0) {
//                                    //不删除
//                                    //添加成功记录
//                                    dataSourceTransactionManager.rollback(MonthsStatus);
//                                    recordsheet.setStatus(1);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//
//                                    recordsheet.setSpendtime(spendTime);
//                                    baseMapper.updateById(recordsheet);
//                                    //                                ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    return "成功，不删除原始数据";
//                                } else {
//                                    //删除
//                                    try {
//                                        List<Long> allIdList = baseMapper.allCustomId(tableIdName, databaseName, tableName, fieldTime, customStart, customEnd);
//
//                                        int i = delCustomSure(databaseName, tableName, tableIdName, allIdList);
//
//                                        if (i == 0) {
//                                            //没有删除数据
//                                            dataSourceTransactionManager.rollback(MonthsStatus);
//                                            //删除数据失败，失败记录
//                                            recordsheet.setStatus(0);
//                                            recordsheet.setEndtime(new Date());
//                                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//
//                                            recordsheet.setSpendtime(spendTime);
//                                            baseMapper.updateById(recordsheet);
//                                            //                                ws发送消息
//                                            JqWebSocket.sendMessage();
//                                            return "删除失败";
//                                        }
//
//                                        recordsheet.setStatus(1);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                        recordsheet.setSpendtime(spendTime);
//                                        baseMapper.updateById(recordsheet);
//                                        dataSourceTransactionManager.commit(MonthsStatus);
//                                        //                                ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "成功添加表并删除了原始数据";
//                                    } catch (Exception n) {
//                                        System.out.println("n的报错" + n.getMessage());
//                                        dataSourceTransactionManager.rollback(MonthsStatus);
//                                        //异常错误，失败记录
//                                        recordsheet.setStatus(0);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                        recordsheet.setSpendtime(spendTime);
//                                        //插入失败的记录
//                                        baseMapper.updateById(recordsheet);
//                                        //                                ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "原始数据删除失败，异常错误";
//                                    }
//                                }
                            }
                            //提交事务
                            dataSourceTransactionManager.commit(MonthsStatus);
                        } catch (Exception e1) {
                            dataSourceTransactionManager.rollback(MonthsStatus);
                            //异常错误，失败记录
                            recordsheet.setStatus(0);
                            recordsheet.setEndtime(new Date());
                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                            recordsheet.setSpendtime(spendTime);
                            //插入失败的记录
                            baseMapper.updateById(recordsheet);
                            //失败后，判断数据库是否存在，存在则插入数据库，不存在则不插入
//                            if (wb == 0) {
//                                datasourceMapper.insert(datasource);
//                            }
                            //                                ws发送消息
                            JqWebSocket.sendMessage();
                            System.out.println("e1的报错" + e1.getMessage());
                            return "添加表失败，请检查填写是否错误";
                        }
                    }

                }else if (endType==3){
//                System.out.println("开始时间"+customStart);
//                System.out.println("自定义结束时间"+customEnd);
                    //周
//                System.out.println("自定义 周");

//                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");//设置日期格式
//                    String nowName = df.format(new Date());
                    SimpleDateFormat dbdate = new SimpleDateFormat("yyMMddHHmmss");//设置日期格式
                    String dbstr = dbdate.format(new Date());

                    newdataName = databaseName + "_cs_"+customField+dbstr;

                    //路径
                    filePath = filePre + newdataName + "/";

                    wb = whetherBase(newdataName);

                    //数据库存在，就是wb=1
                    if (wb == 0) {
                        //数据库不存在，则先创建数据库
                        baseMapper.createLibrary(newdataName);
                        //随后插入数据库
                        //设置添加数据库的名字和URL
//                        datasource.setDsname(newdataName);
//                        datasource.setUrl(PRE + newdataName + Suffix);
//                        datasourceMapper.insert(datasource);
                    }

                    recordsheet.setNewdataName(newdataName);
                    recordsheet.setStatus(2);
                    recordsheet.setTime(new Date());
//                TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                    int insert = baseMapper.insert(recordsheet);
//                System.out.println("插入状态：" + insert);
                    while (true) {
//                开启事务
                        TransactionStatus WeeksStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                        try {
                            Map<String, List<Long>> TbMap = new HashMap<>();
                            //数据库查询数据
                            List<Long> list = baseMapper.customData(databaseName,tableName,tableIdName,fieldTime,customStart,customEnd,offset,pageSize);

                            //判断是否有数据
                            boolean empty = CollectionUtils.isNotEmpty(list);
                            if (offset==0&&empty==false){
                                dataSourceTransactionManager.rollback(WeeksStatus);
                                //异常错误，失败记录
                                recordsheet.setStatus(0);
                                recordsheet.setEndtime(new Date());
                                String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                                recordsheet.setSpendtime(spendTime);
                                //插入失败的记录
                                baseMapper.updateById(recordsheet);
                                //                            ws发送消息
                                JqWebSocket.sendMessage();
                                return "此时间段无数据";
                            }
                            offset += pageSize;

                            if (empty) {
                                //分页有值
                                //判断目标表是否存在
                                Integer finRes = 0;

                                for (int i = 0; i < list.size(); i++) {

//                                //2.判断哪一年的：
//                                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                                    //得到当前id对应的时间字段 yyyy年份
                                    Long id = list.get(i);
                                    //得到数据库Date类型时间值
//                                    Date date = baseMapper.findDate(fieldTime, databaseName, tableName, tableIdName, id);
                                    FieldGet fieldGet = baseMapper.fieldGet(fieldTime, customField, databaseName, tableName, tableIdName, id);
                                    Date date = fieldGet.getFieldTime();

                                    int tinyInt = fieldGet.getCustomField();

                                    Calendar calendar = Calendar.getInstance();
                                    calendar.setFirstDayOfWeek(Calendar.MONDAY);
                                    calendar.setTime(date);
                                    int week = calendar.get(Calendar.WEEK_OF_YEAR);

                                    Name = newtableName; // 需要打印中间代理的Name

                                    //3.设置目标表名：格式、例：finance_record_tb_week_52_type1
                                    newtableName = tableName+ "_week_" + week+"_"+customField+tinyInt;
//                                System.out.println("周表名："+newtableName);

                                    //4.判断目标表是否存在
                                    wt = baseMapper.whetherTable(newdataName, newtableName);
                                    if (wt == 0) {
                                        //表不存在，则先创建表
                                        baseMapper.addTable(newdataName, newtableName, databaseName, tableName);
                                    }

                                    boolean result = newtableName.equals(Name);
//                                System.out.println("前面是否等于后面："+result);
                                    if (result == false && Name != null) {
                                        //如果前面表不等于后面的， 先判断map里有没有这个表key
                                        boolean b = TbMap.containsKey(newtableName);
                                        //如果有
                                        if (b == true) {//添加id到这个表中

                                            TbMap.get(newtableName).add(id);
//                                        System.out.println("有这个key：" + newtableName);
                                        } else {
                                            //如果没有，就new一个list放id 存map
//                                        List<Long> tableIdList= new ArrayList<>();
//                                        tableIdList.add(id);
                                            TbMap.put(newtableName, new ArrayList<>());
                                            TbMap.get(newtableName).add(id);
//                                        System.out.println("没有这个key，这里的name" + Name + "，这里的新表名：" + newtableName);
                                        }
                                    } else {
                                        boolean c = TbMap.containsKey(newtableName);
//                                    System.out.println("是否有"+newtableName+"::"+c);
                                        if (c == false) {
                                            //如果表名相等,直接添加id
                                            TbMap.put(newtableName, new ArrayList<>());
                                            TbMap.get(newtableName).add(id);
                                        } else {
                                            TbMap.get(newtableName).add(id);
                                        }
                                    }
                                }
                                for (Map.Entry<String, List<Long>> entry : TbMap.entrySet()) {
                                    try {
                                        //5.放数据
                                        int result = testInsert(newdataName, entry.getKey(), databaseName, tableName, tableIdName, entry.getValue());
                                        finRes += result;
                                    } catch (RuntimeException e) {
                                        if (e instanceof DuplicateKeyException) {
                                            System.out.println("该接口数据主键冲突" + e.getMessage());
//                                    dataSourceTransactionManager.rollback(transactionStatus);
                                            //这页的第一个数据主键已存在，则跳过当前页循环，进行下一页数据插入
                                            break;
                                        } else {
                                            dataSourceTransactionManager.rollback(WeeksStatus);
                                            System.out.println("其他错误" + e.getMessage());
                                            //异常错误，失败记录
                                            recordsheet.setStatus(0);
                                            recordsheet.setEndtime(new Date());
                                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                                            recordsheet.setSpendtime(spendTime);
                                            //插入失败的记录
                                            baseMapper.updateById(recordsheet);
                                            //                            ws发送消息
                                            JqWebSocket.sendMessage();
                                            return "异常错误";
                                        }
                                    }
                                }
//                            ws发送消息
                                JqWebSocket.sendMessage();

                                System.out.println("list==" + empty);
                            } else {
                                //做最后一个表的备份
//                            mysqlFileName = Name + ".sql";
//                            mysqlBackupsService.mysqlBackups(filePath,newdataName,Name,mysqlFileName,userName,passWord);
                                //最后分完做备份
                                List<String> TabNamels = baseMapper.ShowAllTB(newdataName);

                                for (int i = 0; i < TabNamels.size(); i++) {
                                    mysqlFileName = TabNamels.get(i) + ".sql";
                                    baseMapper.rename(tableName,TabNamels.get(i),newdataName);

                                    mysqlBackupsService.mysqlBackups(Ip,filePath, newdataName, tableName, mysqlFileName, userName, passWord);
                                    //做删除操作
                                    baseMapper.delTB(newdataName,tableName);
                                    System.out.println("备份完毕,删除一个已备份的表");
                                }

                                //判断数据库中是否还有表，没有，则删除库，有则不做处理
                                List<String> allTb = baseMapper.ShowAllTB(newdataName);
                                if (allTb.isEmpty()){
                                    //判断数据库是否有表，如果为空，就删除该数据库
                                    baseMapper.delDataBase(newdataName);
                                }
                                //集合里没有数据了,到最后了，状态为：1.成功
                                //分页无值
                                //0.不删除，1.删除
//                                if (sure == 0) {
//                                    //不删除
//                                    //添加成功记录
//                                    dataSourceTransactionManager.rollback(WeeksStatus);
//                                    recordsheet.setStatus(1);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                    recordsheet.setSpendtime(spendTime);
//                                    baseMapper.updateById(recordsheet);
//                                    //                                ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    return "成功，不删除原始数据";
//                                } else {
//                                    //删除
//                                    try {
//                                        List<Long> allIdList = baseMapper.allCustomId(tableIdName, databaseName, tableName, fieldTime, customStart, customEnd);
//
//                                        int i = delCustomSure(databaseName, tableName, tableIdName, allIdList);
//
//                                        if (i == 0) {
//                                            //没有删除数据
//                                            dataSourceTransactionManager.rollback(WeeksStatus);
//                                            //删除数据失败，失败记录
//                                            recordsheet.setStatus(0);
//                                            recordsheet.setEndtime(new Date());
//                                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//
//                                            recordsheet.setSpendtime(spendTime);
//                                            baseMapper.updateById(recordsheet);
//                                            //                                ws发送消息
//                                            JqWebSocket.sendMessage();
//                                            return "删除失败";
//                                        }
//
//                                        recordsheet.setStatus(1);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                        recordsheet.setSpendtime(spendTime);
//                                        baseMapper.updateById(recordsheet);
//                                        dataSourceTransactionManager.commit(WeeksStatus);
//                                        //                                ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "成功添加表并删除了原始数据";
//
//                                    } catch (Exception n) {
//                                        System.out.println("n的报错" + n.getMessage());
//                                        dataSourceTransactionManager.rollback(WeeksStatus);
//                                        //异常错误，失败记录
//                                        recordsheet.setStatus(0);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                        recordsheet.setSpendtime(spendTime);
//                                        //插入失败的记录
//                                        baseMapper.updateById(recordsheet);
//                                        //                                ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "原始数据删除失败，异常错误";
//                                    }
//                                }
                            }
                            //提交事务
                            dataSourceTransactionManager.commit(WeeksStatus);
                        } catch (Exception e1) {
                            dataSourceTransactionManager.rollback(WeeksStatus);
                            //异常错误，失败记录
                            recordsheet.setStatus(0);
                            recordsheet.setEndtime(new Date());
                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                            recordsheet.setSpendtime(spendTime);
                            //插入失败的记录
                            baseMapper.updateById(recordsheet);
                            //失败后，判断数据库是否存在，存在则插入数据库，不存在则不插入
//                            if (wb == 0) {
//                                datasourceMapper.insert(datasource);
//                            }
                            //                                ws发送消息
                            JqWebSocket.sendMessage();
                            System.out.println("e1的报错" + e1.getMessage());
                            return "添加表失败，请检查填写是否错误";
                        }
                    }
                }else if (endType==4){
                    //日
//                System.out.println("自定义 日");


//                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");//设置日期格式
//                    String nowName = df.format(new Date());
                    SimpleDateFormat dbdate = new SimpleDateFormat("yyMMddHHmmss");//设置日期格式
                    String dbstr = dbdate.format(new Date());

                    newdataName = databaseName + "_cs_"+customField+dbstr;

                    //路径
                    filePath = filePre + newdataName + "/";

                    wb = whetherBase(newdataName);

                    //数据库存在，就是wb=1
                    if (wb == 0) {
                        //数据库不存在，则先创建数据库
                        baseMapper.createLibrary(newdataName);
                        //随后插入数据库
                        //设置添加数据库的名字和URL
//                        datasource.setDsname(newdataName);
//                        datasource.setUrl(PRE + newdataName + Suffix);
//                        datasourceMapper.insert(datasource);
                    }

                    recordsheet.setNewdataName(newdataName);
                    recordsheet.setStatus(2);
                    recordsheet.setTime(new Date());
//                TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                    int insert = baseMapper.insert(recordsheet);
//                System.out.println("插入状态：" + insert);
                    while (true) {
//                开启事务
                        TransactionStatus DaysStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                        try {
                            Map<String, List<Long>> TbMap = new HashMap<>();
                            //数据库查询数据
                            List<Long> list = baseMapper.customData(databaseName, tableName, tableIdName, fieldTime, customStart, customEnd, offset, pageSize);

                            //判断是否有数据
                            boolean empty = CollectionUtils.isNotEmpty(list);
                            if (offset==0&&empty==false){
                                dataSourceTransactionManager.rollback(DaysStatus);
                                //删除数据失败，失败记录
                                recordsheet.setStatus(0);
                                recordsheet.setEndtime(new Date());
                                String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                                recordsheet.setSpendtime(spendTime);
                                baseMapper.updateById(recordsheet);
                                //                                ws发送消息
                                JqWebSocket.sendMessage();
                                return "此时间段无数据";
                            }
                            offset += pageSize;

                            if (empty) {
                                //分页有值
                                //判断目标表是否存在
                                Integer finRes = 0;

                                for (int i = 0; i < list.size(); i++) {
                                    //2.判断哪一年的：
                                    SimpleDateFormat format = new SimpleDateFormat("yyyy_MM_dd");
                                    //得到当前id对应的时间字段 yyyy年份
                                    Long id = list.get(i);
                                    //得到数据库Date类型时间值

//                                    Date date = baseMapper.findDate(fieldTime, databaseName, tableName, tableIdName, id);
//                                    String day = format.format(date);
                                    FieldGet fieldGet = baseMapper.fieldGet(fieldTime, customField, databaseName, tableName, tableIdName, id);
                                    Date date = fieldGet.getFieldTime();
                                    String day = format.format(date);

                                    int tinyInt = fieldGet.getCustomField();


                                    Name = newtableName; // 需要打印中间代理的Name

                                    //3.设置目标表名：格式、例：finance_record_tb_天
                                    newtableName = tableName + day+"_"+customField+tinyInt;
                                    //4.判断目标表是否存在
                                    wt = baseMapper.whetherTable(newdataName, newtableName);
                                    if (wt == 0) {
                                        //表不存在，则先创建表
                                        baseMapper.addTable(newdataName, newtableName, databaseName, tableName);
                                    }

                                    boolean result = newtableName.equals(Name);
//                                System.out.println("前面是否等于后面："+result);
                                    if (result == false && Name != null) {
                                        //如果前面表不等于后面的， 先判断map里有没有这个表key
                                        boolean b = TbMap.containsKey(newtableName);
                                        //如果有
                                        if (b == true) {//添加id到这个表中

                                            TbMap.get(newtableName).add(id);
//                                        System.out.println("有这个key：" + newtableName);
                                        } else {
                                            //如果没有，就new一个list放id 存map
//                                        List<Long> tableIdList= new ArrayList<>();
//                                        tableIdList.add(id);
                                            TbMap.put(newtableName, new ArrayList<>());
                                            TbMap.get(newtableName).add(id);
//                                        System.out.println("没有这个key，这里的name" + Name + "，这里的新表名：" + newtableName);
                                        }
                                    } else {
                                        boolean c = TbMap.containsKey(newtableName);
//                                    System.out.println("是否有"+newtableName+"::"+c);
                                        if (c == false) {
                                            //如果表名相等,直接添加id
                                            TbMap.put(newtableName, new ArrayList<>());
                                            TbMap.get(newtableName).add(id);
                                        } else {
                                            TbMap.get(newtableName).add(id);
                                        }
                                    }
                                }
                                for (Map.Entry<String, List<Long>> entry : TbMap.entrySet()) {
                                    try {
                                        //5.放数据
                                        int result = testInsert(newdataName, entry.getKey(), databaseName, tableName, tableIdName, entry.getValue());
                                        finRes += result;
                                    } catch (RuntimeException e) {
                                        if (e instanceof DuplicateKeyException) {
                                            System.out.println("该接口数据主键冲突" + e.getMessage());
//                                    dataSourceTransactionManager.rollback(transactionStatus);
                                            //这页的第一个数据主键已存在，则跳过当前页循环，进行下一页数据插入
                                            break;
                                        } else {
                                            dataSourceTransactionManager.rollback(DaysStatus);
                                            System.out.println("其他错误" + e.getMessage());
                                            //异常错误，失败记录
                                            recordsheet.setStatus(0);
                                            recordsheet.setEndtime(new Date());
                                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                                            recordsheet.setSpendtime(spendTime);
                                            //插入失败的记录
                                            baseMapper.updateById(recordsheet);
                                            //                            ws发送消息
                                            JqWebSocket.sendMessage();
                                            return "异常错误";
                                        }
                                    }
                                }
                                //                            ws发送消息
                                JqWebSocket.sendMessage();

//                            System.out.println("最终成功数：" + finRes);
                                System.out.println("list==" + empty);
                            } else {
                                //做最后一个表的备份
//                            mysqlFileName = Name + ".sql";
//                            mysqlBackupsService.mysqlBackups(filePath,newdataName,Name,mysqlFileName,userName,passWord);
                                //最后分完做备份
                                List<String> TabNamels = baseMapper.ShowAllTB(newdataName);

                                for (int i = 0; i < TabNamels.size(); i++) {
                                    mysqlFileName = TabNamels.get(i) + ".sql";
                                    baseMapper.rename(tableName,TabNamels.get(i),newdataName);

                                    mysqlBackupsService.mysqlBackups(Ip,filePath, newdataName, tableName, mysqlFileName, userName, passWord);
                                    //做删除操作
                                    baseMapper.delTB(newdataName,tableName);
                                    System.out.println("备份完毕,删除一个已备份的表");
                                }


                                //判断数据库中是否还有表，没有，则删除库，有则不做处理
                                List<String> allTb = baseMapper.ShowAllTB(newdataName);
                                if (allTb.isEmpty()){
                                    //判断数据库是否有表，如果为空，就删除该数据库
                                    baseMapper.delDataBase(newdataName);
                                }

                                //集合里没有数据了,到最后了，状态为：1.成功
                                //分页无值
                                //0.不删除，1.删除
//                                if (sure == 0) {
//                                    //不删除
//                                    //添加成功记录
//                                    dataSourceTransactionManager.rollback(DaysStatus);
//                                    recordsheet.setStatus(1);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                    recordsheet.setSpendtime(spendTime);
//                                    baseMapper.updateById(recordsheet);
//                                    return "成功，不删除原始数据";
//                                } else {
//                                    //删除
//                                    try {
//
//                                        List<Long> allIdList = baseMapper.allCustomId(tableIdName, databaseName, tableName, fieldTime, customStart, customEnd);
//
//                                        int i = delCustomSure(databaseName, tableName, tableIdName, allIdList);
//
//                                        if (i == 0) {
//                                            //没有删除数据
//                                            dataSourceTransactionManager.rollback(DaysStatus);
//                                            //删除数据失败，失败记录
//                                            recordsheet.setStatus(0);
//                                            recordsheet.setEndtime(new Date());
//                                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//
//                                            recordsheet.setSpendtime(spendTime);
//                                            baseMapper.updateById(recordsheet);
//                                            //                                ws发送消息
//                                            JqWebSocket.sendMessage();
//                                            return "删除失败";
//                                        }
//
//                                        recordsheet.setStatus(1);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                        recordsheet.setSpendtime(spendTime);
//                                        baseMapper.updateById(recordsheet);
//                                        dataSourceTransactionManager.commit(DaysStatus);
//                                        //                                ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "成功添加表并删除了原始数据";
//
//                                    } catch (Exception n) {
//                                        System.out.println("n的报错" + n.getMessage());
//                                        dataSourceTransactionManager.rollback(DaysStatus);
//                                        //异常错误，失败记录
//                                        recordsheet.setStatus(0);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                        recordsheet.setSpendtime(spendTime);
//                                        //插入失败的记录
//                                        baseMapper.updateById(recordsheet);
//                                        //                                ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "原始数据删除失败，异常错误";
//                                    }
//                                }
                            }
                            //提交事务
                            dataSourceTransactionManager.commit(DaysStatus);
                        } catch (Exception e1) {
                            dataSourceTransactionManager.rollback(DaysStatus);
                            //异常错误，失败记录
                            recordsheet.setStatus(0);
                            recordsheet.setEndtime(new Date());
                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                            recordsheet.setSpendtime(spendTime);
                            //插入失败的记录
                            baseMapper.updateById(recordsheet);
                            //失败后，判断数据库是否存在，存在则插入数据库，不存在则不插入
//                            if (wb == 0) {
//                                datasourceMapper.insert(datasource);
//                            }
                            //                                ws发送消息
                            JqWebSocket.sendMessage();
                            System.out.println("e1的报错" + e1.getMessage());
                            return "添加表失败，请检查填写是否错误";
                        }
                    }
                }
            } else {
                //自定义为空，type为空，时间字段为空时，分类字段不为空
                //分类字段单独分,时间字段为空
                if (type == null && fieldTime==null) {
                    //============================分类字段
                        //添加进行中记录

//                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");//设置日期格式
//                    String nowName = df.format(new Date());
                    SimpleDateFormat dbdate = new SimpleDateFormat("yyMMddHHmmss");//设置日期格式
                    String dbstr = dbdate.format(new Date());

                    newdataName = databaseName + "_"+customField+dbstr;

                    //路径
                    filePath = filePre + newdataName + "/";


                        recordsheet.setNewdataName(newdataName);
                        recordsheet.setStatus(2);
                        recordsheet.setTime(new Date());
//                TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                        int insert = baseMapper.insert(recordsheet);

                        while (true) {
//                开启事务
                            TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);

                            try {
                                Map<String, List<Long>> TbMap = new HashMap<>();
                                //数据库查询数据
                                List<Long> list = PagData(databaseName, tableName, tableIdName, offset, pageSize);

                                offset += pageSize;
                                //判断是否有数据
                                boolean empty = CollectionUtils.isNotEmpty(list);
                                if (empty) {
                                    //分页有值
                                    wb = whetherBase(newdataName);
                                    if (wb == 0) {
                                        //数据库不存在，则先创建数据库
                                        baseMapper.createLibrary(newdataName);

                                    }
                                    //数据库存在，就是wb=1
                                    //判断目标表是否存在
                                    Integer finRes = 0;

                                    // 一页一页的处理
                                    for (int i = 0; i < list.size(); i++) {

                                        //得到当前id对应的字段值
                                        Long id = list.get(i);

                                        int tinyInt = baseMapper.findTinyInt(customField, databaseName, tableName, tableIdName, id);

                                        Name = newtableName; // 需要打印中间代理的Name

                                        //3.设置目标表名：格式、例：finance_record_tb年
                                        newtableName = tableName+ "_"+customField+tinyInt;

                                        //4.判断目标表是否存在
                                        wt = baseMapper.whetherTable(newdataName, newtableName);
                                        if (wt == 0) {
                                            //表不存在，则先创建表
                                            baseMapper.addTable(newdataName, newtableName, databaseName, tableName);
                                        }

                                        boolean result = newtableName.equals(Name);
//                                System.out.println("前面是否等于后面："+result);
                                        if (result == false && Name != null) {
                                            //如果前面表不等于后面的， 先判断map里有没有这个表key
                                            boolean b = TbMap.containsKey(newtableName);
                                            //如果有
                                            if (b == true) {//添加id到这个表中

                                                TbMap.get(newtableName).add(id);
//                                        System.out.println("有这个key："+newtableName);
                                            } else {
                                                //如果没有，就new一个list放id 存map
//                                        List<Long> tableIdList= new ArrayList<>();
//                                        tableIdList.add(id);
                                                TbMap.put(newtableName, new ArrayList<>());
                                                TbMap.get(newtableName).add(id);
//                                        System.out.println("没有这个key，这里的name"+Name+"，这里的新表名："+newtableName);
                                            }
                                        } else {
                                            boolean c = TbMap.containsKey(newtableName);
//                                    System.out.println("是否有"+newtableName+"::"+c);
                                            if (c == false) {
                                                //如果表名相等,直接添加id
                                                TbMap.put(newtableName, new ArrayList<>());
                                                TbMap.get(newtableName).add(id);
                                            } else {
                                                TbMap.get(newtableName).add(id);
                                            }
                                        }
                                    }
                                    for (Map.Entry<String, List<Long>> entry : TbMap.entrySet()) {
                                        try {
                                            //5.放数据
                                            int result = testInsert(newdataName, entry.getKey(), databaseName, tableName, tableIdName, entry.getValue());
                                            finRes += result;

                                        } catch (RuntimeException e) {
                                            if (e instanceof DuplicateKeyException) {
                                                System.out.println("该接口数据主键冲突" + e.getMessage());
                                                //                            ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    dataSourceTransactionManager.rollback(transactionStatus);
                                                //这页的第一个数据主键已存在，则跳过当前页循环，进行下一页数据插入
                                                break;
                                            } else {
                                                dataSourceTransactionManager.rollback(transactionStatus);
                                                System.out.println("其他错误" + e.getMessage());
                                                //异常错误，失败记录
                                                recordsheet.setStatus(0);
                                                recordsheet.setEndtime(new Date());
                                                String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                                                recordsheet.setSpendtime(spendTime);
                                                //插入失败的记录
                                                baseMapper.updateById(recordsheet);
                                                //                            ws发送消息
                                                JqWebSocket.sendMessage();
                                                return "异常错误";
                                            }
                                        }
                                    }

//                            ws发送消息
                                    JqWebSocket.sendMessage();

                                    System.out.println("list==" + empty);
                                } else {
                                    //做最后一个表的备份
                                    //最后分完做备份
                                    //显示所有表
                                    List<String> TabNamels = baseMapper.ShowAllTB(newdataName);

                                    for (int i = 0; i < TabNamels.size(); i++) {
                                        mysqlFileName = TabNamels.get(i) + ".sql";
                                        baseMapper.rename(tableName,TabNamels.get(i),newdataName);

                                        mysqlBackupsService.mysqlBackups(Ip,filePath, newdataName, tableName, mysqlFileName, userName, passWord);
                                        //做删除操作
                                        baseMapper.delTB(newdataName,tableName);
                                        System.out.println("备份完毕,删除一个已备份的表");
                                    }

                                    //判断数据库中是否还有表，没有，则删除库，有则不做处理
                                    List<String> allTb = baseMapper.ShowAllTB(newdataName);
                                    if (allTb.isEmpty()){
                                        //判断数据库是否有表，如果为空，就删除该数据库
                                        baseMapper.delDataBase(newdataName);
                                    }

                                    //集合里没有数据了,到最后了，状态为：1.成功
                                    //分页无值
                                    //0.不删除，1.删除
//                                    if (sure == 0) {
//                                        //不删除
//                                        //添加成功记录
//                                        dataSourceTransactionManager.rollback(transactionStatus);
//                                        //结束，更改记录数据
//                                        recordsheet.setStatus(1);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                        recordsheet.setSpendtime(spendTime);
//                                        baseMapper.updateById(recordsheet);
////                                ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "成功，不删除原始数据";
//                                    } else {
//                                        //删除
//                                        try {
//                                            Boolean aBoolean = delSure(databaseName, tableName, tableIdName);
//                                            if (aBoolean == false) {
//                                                dataSourceTransactionManager.rollback(transactionStatus);
//                                                //删除数据失败，失败记录
//                                                recordsheet.setStatus(0);
//                                                recordsheet.setEndtime(new Date());
//                                                String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//
//                                                recordsheet.setSpendtime(spendTime);
//                                                baseMapper.updateById(recordsheet);
//                                                //                                ws发送消息
//                                                JqWebSocket.sendMessage();
//                                                return "删除失败";
//                                            }
//                                            recordsheet.setStatus(1);
//                                            recordsheet.setEndtime(new Date());
//                                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                            recordsheet.setSpendtime(spendTime);
//                                            baseMapper.updateById(recordsheet);
//                                            dataSourceTransactionManager.commit(transactionStatus);
//                                            //  ws发送消息
//                                            JqWebSocket.sendMessage();
//                                            return "成功添加表并删除了原始数据";
//                                        } catch (Exception n) {
//                                            System.out.println("n的报错" + n.getMessage());
//                                            dataSourceTransactionManager.rollback(transactionStatus);
//                                            //异常错误，失败记录
//                                            recordsheet.setStatus(0);
//                                            recordsheet.setEndtime(new Date());
//                                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//
//                                            recordsheet.setSpendtime(spendTime);
//                                            //插入失败的记录
//                                            baseMapper.updateById(recordsheet);
//                                            //                                ws发送消息
//                                            JqWebSocket.sendMessage();
//                                            return "原始数据删除失败，异常错误";
//                                        }
//                                    }
                                }
                                //提交事务
                                dataSourceTransactionManager.commit(transactionStatus);

                            } catch (Exception e1) {
                                dataSourceTransactionManager.rollback(transactionStatus);
                                //异常错误，失败记录
                                recordsheet.setStatus(0);
                                recordsheet.setEndtime(new Date());
                                String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                                recordsheet.setSpendtime(spendTime);
                                //插入失败的记录
                                baseMapper.updateById(recordsheet);
                                //失败后，判断数据库是否存在，存在则插入数据库，不存在则不插入
//                                if (wb == 0) {
//                                    datasourceMapper.insert(datasource);
//                                }
                                //                                ws发送消息
                                JqWebSocket.sendMessage();
                                System.out.println("e1的报错" + e1.getMessage());
                                return "添加表失败，请检查填写是否错误";
                            }
                        }

                }else if (type==null&& fieldTime!=null){
                    //自定义为空，type为空，时间字段不为空时，分类字段不为空
                    return "请选择模式";
                }
                //自定义时间为空时,type不为空,分类字段也不为空
                //type=0年，type=1月，
                //=============================================分类字段与type时间字段
                if (type == 0) {
                    //1.查询1000条，判断是哪一年的，放入对应的年表，存在就直接放，不存在，创建数据库，在放。(时间段在当年)
                    //2.分页查询 asc
                    //按年分
                    //开始
                    //添加进行中记录

//                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");//设置日期格式
//                    String nowName = df.format(new Date());
                    SimpleDateFormat dbdate = new SimpleDateFormat("yyMMddHHmmss");//设置日期格式
                    String dbstr = dbdate.format(new Date());

                    newdataName = databaseName + "_year_"+customField+dbstr;

                    //路径
                    filePath = filePre + newdataName + "/";

                    recordsheet.setNewdataName(newdataName);
                    recordsheet.setStatus(2);
                    recordsheet.setTime(new Date());
//                TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                    int insert = baseMapper.insert(recordsheet);

//                System.out.println("插入状态：" + insert);
                    while (true) {
//                开启事务
                        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);

                        try {
                            Map<String, List<Long>> TbMap = new HashMap<>();
                            //数据库查询数据
                            List<Long> list = PagData(databaseName, tableName, tableIdName, offset, pageSize);

                            offset += pageSize;
                            //判断是否有数据
                            boolean empty = CollectionUtils.isNotEmpty(list);
                            if (empty) {
                                //分页有值
                                wb = whetherBase(newdataName);
                                if (wb == 0) {
                                    //数据库不存在，则先创建数据库
                                    baseMapper.createLibrary(newdataName);

                                    //随后插入数据库
                                    //设置添加数据库的名字和URL
//                                    datasource.setDsname(newdataName);
//                                    datasource.setUrl(PRE + newdataName + Suffix);
//                                    datasourceMapper.insert(datasource);
                                }
                                //数据库存在，就是wb=1
                                //判断目标表是否存在
                                Integer finRes = 0;

                                // 一页一页的处理
                                for (int i = 0; i < list.size(); i++) {
                                    //2.判断哪一年的：
                                    SimpleDateFormat format = new SimpleDateFormat("yyyy");

                                    //得到当前id对应的时间字段 yyyy年份
                                    Long id = list.get(i);
                                    //得到数据库Date类型时间值

//                                    Date date = baseMapper.findDate(fieldTime, databaseName, tableName, tableIdName, id);
//                                    String year = format.format(date);
                                    FieldGet fieldGet = baseMapper.fieldGet(fieldTime, customField, databaseName, tableName, tableIdName, id);
                                    Date date = fieldGet.getFieldTime();
                                    String year = format.format(date);

                                    int tinyInt = fieldGet.getCustomField();

                                    Name = newtableName; // 需要打印中间代理的Name

                                    //3.设置目标表名：格式、例：finance_record_tb年
                                    newtableName = tableName + year + "_"+customField+tinyInt;

                                    //4.判断目标表是否存在
                                    wt = baseMapper.whetherTable(newdataName, newtableName);
                                    if (wt == 0) {
                                        //表不存在，则先创建表
                                        baseMapper.addTable(newdataName, newtableName, databaseName, tableName);
                                    }

                                    boolean result = newtableName.equals(Name);
//                                System.out.println("前面是否等于后面："+result);
                                    if (result == false && Name != null) {
                                        //如果前面表不等于后面的， 先判断map里有没有这个表key
                                        boolean b = TbMap.containsKey(newtableName);
                                        //如果有
                                        if (b == true) {//添加id到这个表中

                                            TbMap.get(newtableName).add(id);
//                                        System.out.println("有这个key："+newtableName);
                                        } else {
                                            //如果没有，就new一个list放id 存map
//                                        List<Long> tableIdList= new ArrayList<>();
//                                        tableIdList.add(id);
                                            TbMap.put(newtableName, new ArrayList<>());
                                            TbMap.get(newtableName).add(id);
//                                        System.out.println("没有这个key，这里的name"+Name+"，这里的新表名："+newtableName);
                                        }
                                    } else {
                                        boolean c = TbMap.containsKey(newtableName);
//                                    System.out.println("是否有"+newtableName+"::"+c);
                                        if (c == false) {
                                            //如果表名相等,直接添加id
                                            TbMap.put(newtableName, new ArrayList<>());
                                            TbMap.get(newtableName).add(id);
                                        } else {
                                            TbMap.get(newtableName).add(id);
                                        }
                                    }
                                }
                                for (Map.Entry<String, List<Long>> entry : TbMap.entrySet()) {
                                    try {
                                        //5.放数据
                                        //得到id
                                        //假装出错 放数据时
//                                int d = 1/0;
//                                int result = insertData(newdataName, newtableName,databaseName,tableName,tableIdName,id);
//                                    + ", Value = " + entry.getValue()
//                                    System.out.println("Key = " + entry.getKey());
                                        int result = testInsert(newdataName, entry.getKey(), databaseName, tableName, tableIdName, entry.getValue());
//                                    System.out.println("插入+1");
                                        finRes += result;

                                    } catch (RuntimeException e) {
                                        if (e instanceof DuplicateKeyException) {
                                            System.out.println("该接口数据主键冲突" + e.getMessage());
                                            //                            ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    dataSourceTransactionManager.rollback(transactionStatus);
                                            //这页的第一个数据主键已存在，则跳过当前页循环，进行下一页数据插入
                                            break;
                                        } else {
                                            dataSourceTransactionManager.rollback(transactionStatus);
                                            System.out.println("其他错误" + e.getMessage());
                                            //异常错误，失败记录
                                            recordsheet.setStatus(0);
                                            recordsheet.setEndtime(new Date());
                                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                                            recordsheet.setSpendtime(spendTime);
                                            //插入失败的记录
                                            baseMapper.updateById(recordsheet);
                                            //                            ws发送消息
                                            JqWebSocket.sendMessage();
                                            return "异常错误";
                                        }
                                    }
                                }

//                            ws发送消息
                                JqWebSocket.sendMessage();

                                System.out.println("list==" + empty);
                            } else {
                                //做最后一个表的备份
                                //最后分完做备份
                                List<String> TabNamels = baseMapper.ShowAllTB(newdataName);

                                for (int i = 0; i < TabNamels.size(); i++) {
                                    mysqlFileName = TabNamels.get(i) + ".sql";
                                    baseMapper.rename(tableName,TabNamels.get(i),newdataName);

                                    mysqlBackupsService.mysqlBackups(Ip,filePath, newdataName, tableName, mysqlFileName, userName, passWord);
                                    //做删除操作
                                    baseMapper.delTB(newdataName,tableName);
                                    System.out.println("备份完毕,删除一个已备份的表");
                                }

                                //判断数据库中是否还有表，没有，则删除库，有则不做处理
                                List<String> allTb = baseMapper.ShowAllTB(newdataName);
                                if (allTb.isEmpty()){
                                    //判断数据库是否有表，如果为空，就删除该数据库
                                    baseMapper.delDataBase(newdataName);
                                }

                                //集合里没有数据了,到最后了，状态为：1.成功
                                //分页无值
                                //0.不删除，1.删除
//                                if (sure == 0) {
//                                    //不删除
//                                    //添加成功记录
//                                    dataSourceTransactionManager.rollback(transactionStatus);
//                                    //结束，更改记录数据
//                                    recordsheet.setStatus(1);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                    recordsheet.setSpendtime(spendTime);
//                                    baseMapper.updateById(recordsheet);
////                                ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    return "成功，不删除原始数据";
//                                } else {
//                                    //删除
//                                    try {
//                                        Boolean aBoolean = delSure(databaseName, tableName, tableIdName);
//                                        if (aBoolean == false) {
//                                            dataSourceTransactionManager.rollback(transactionStatus);
//                                            //删除数据失败，失败记录
//                                            recordsheet.setStatus(0);
//                                            recordsheet.setEndtime(new Date());
//                                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//
//                                            recordsheet.setSpendtime(spendTime);
//                                            baseMapper.updateById(recordsheet);
//                                            //                                ws发送消息
//                                            JqWebSocket.sendMessage();
//                                            return "删除失败";
//                                        }
//                                        recordsheet.setStatus(1);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                        recordsheet.setSpendtime(spendTime);
//                                        baseMapper.updateById(recordsheet);
//                                        dataSourceTransactionManager.commit(transactionStatus);
//                                        //  ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "成功添加表并删除了原始数据";
//                                    } catch (Exception n) {
//                                        System.out.println("n的报错" + n.getMessage());
//                                        dataSourceTransactionManager.rollback(transactionStatus);
//                                        //异常错误，失败记录
//                                        recordsheet.setStatus(0);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//
//                                        recordsheet.setSpendtime(spendTime);
//                                        //插入失败的记录
//                                        baseMapper.updateById(recordsheet);
//                                        //                                ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "原始数据删除失败，异常错误";
//                                    }
//                                }
                            }
                            //提交事务
                            dataSourceTransactionManager.commit(transactionStatus);

                        } catch (Exception e1) {
                            dataSourceTransactionManager.rollback(transactionStatus);
                            //异常错误，失败记录
                            recordsheet.setStatus(0);
                            recordsheet.setEndtime(new Date());
                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                            recordsheet.setSpendtime(spendTime);
                            //插入失败的记录
                            baseMapper.updateById(recordsheet);
                            //失败后，判断数据库是否存在，存在则插入数据库，不存在则不插入
//                            if (wb == 0) {
//                                datasourceMapper.insert(datasource);
//                            }
                            //                                ws发送消息
                            JqWebSocket.sendMessage();
                            System.out.println("e1的报错" + e1.getMessage());
                            return "添加表失败，请检查填写是否错误";
                        }
                    }
                } else if (type == 1) {
                    //按月分
                    //开始
                    //添加进行中记录

//                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");//设置日期格式
//                    String nowName = df.format(new Date());
                    SimpleDateFormat dbdate = new SimpleDateFormat("yyMMddHHmmss");//设置日期格式
                    String dbstr = dbdate.format(new Date());
                    newdataName = databaseName + "_months_"+customField+dbstr;
                    //路径
                    filePath = filePre + newdataName + "/";

                    recordsheet.setNewdataName(newdataName);
                    recordsheet.setStatus(2);
                    recordsheet.setTime(new Date());
//                TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                    int insert = baseMapper.insert(recordsheet);
//                System.out.println("插入状态：" + insert);
                    while (true) {
//                开启事务
                        TransactionStatus MonthsStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                        try {

                            Map<String, List<Long>> TbMap = new HashMap<>();
                            //数据库查询数据
                            List<Long> list = PagData(databaseName, tableName, tableIdName, offset, pageSize);

                            offset += pageSize;
                            //判断是否有数据
                            boolean empty = CollectionUtils.isNotEmpty(list);
                            if (empty) {
                                //分页有值
                                wb = whetherBase(newdataName);

                                if (wb == 0) {
                                    //数据库不存在，则先创建数据库
                                    baseMapper.createLibrary(newdataName);
                                    //随后插入数据库
                                    //设置添加数据库的名字和URL
//                                    datasource.setDsname(newdataName);
//                                    datasource.setUrl(PRE + newdataName + Suffix);
//                                    datasourceMapper.insert(datasource);
                                }
                                //数据库存在，就是wb=1
                                //判断目标表是否存在
                                Integer finRes = 0;
                                for (int i = 0; i < list.size(); i++) {

                                    //2.判断哪一年的：
                                    SimpleDateFormat format = new SimpleDateFormat("yyyy_MM");
                                    //得到当前id对应的时间字段 yyyy_MM
                                    Long id = list.get(i);
                                    //得到数据库Date类型时间值

//                                    Date date = baseMapper.findDate(fieldTime, databaseName, tableName, tableIdName, id);
//                                    String year_month = format.format(date);

                                    FieldGet fieldGet = baseMapper.fieldGet(fieldTime, customField, databaseName, tableName, tableIdName, id);
                                    Date date = fieldGet.getFieldTime();
                                    String year_month = format.format(date);

                                    int tinyInt = fieldGet.getCustomField();


                                    Name = newtableName; // 需要打印中间代理的Name

                                    //3.设置目标表名：格式、例：finance_record_tb年月
                                    newtableName = tableName + year_month+"_"+customField+tinyInt;

                                    //4.判断目标表是否存在
                                    wt = baseMapper.whetherTable(newdataName, newtableName);
                                    if (wt == 0) {
                                        //表不存在，则先创建表
                                        baseMapper.addTable(newdataName, newtableName, databaseName, tableName);
                                    }
                                    boolean result = newtableName.equals(Name);
                                    if (result == false && Name != null) {
                                        //如果前面表不等于后面的， 先判断map里有没有这个表key
                                        boolean b = TbMap.containsKey(newtableName);
                                        //如果有
                                        if (b == true) {//添加id到这个表中
                                            TbMap.get(newtableName).add(id);
//                                        System.out.println("有这个key：" + newtableName);
                                        } else {
                                            //如果没有，就new一个list放id 存map
//                                        List<Long> tableIdList= new ArrayList<>();
//                                        tableIdList.add(id);
                                            TbMap.put(newtableName, new ArrayList<>());
                                            TbMap.get(newtableName).add(id);
//                                        System.out.println("没有这个key，这里的name" + Name + "，这里的新表名：" + newtableName);
                                        }
                                    } else {
                                        boolean c = TbMap.containsKey(newtableName);
//                                    System.out.println("是否有"+newtableName+"::"+c);
                                        if (c == false) {
                                            //如果表名相等,直接添加id
                                            TbMap.put(newtableName, new ArrayList<>());
                                            TbMap.get(newtableName).add(id);
                                        } else {
                                            TbMap.get(newtableName).add(id);
                                        }
                                    }
                                }
                                for (Map.Entry<String, List<Long>> entry : TbMap.entrySet()) {
                                    try {
                                        //5.放数据
                                        //得到id
                                        //假装出错 放数据时
//                                int d = 1/0;
//                                    int result = baseMapper.insertData(newdataName, newtableName,databaseName,tableName,tableIdName,id);
//                                    finRes += result ;
//                                    System.out.println("Key = " + entry.getKey());
                                        int result = testInsert(newdataName, entry.getKey(), databaseName, tableName, tableIdName, entry.getValue());
//                                    System.out.println("插入+1");
                                        finRes += result;
                                    } catch (RuntimeException e) {
                                        if (e instanceof DuplicateKeyException) {
                                            System.out.println("该接口数据主键冲突" + e.getMessage());
//                                    dataSourceTransactionManager.rollback(transactionStatus);
                                            //这页的第一个数据主键已存在，则跳过当前页循环，进行下一页数据插入
                                            break;
                                        } else {
                                            dataSourceTransactionManager.rollback(MonthsStatus);
                                            System.out.println("其他错误" + e.getMessage());
                                            //异常错误，失败记录
                                            recordsheet.setStatus(0);
                                            recordsheet.setEndtime(new Date());
                                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                                            recordsheet.setSpendtime(spendTime);
                                            //插入失败的记录
                                            baseMapper.updateById(recordsheet);
                                            //                            ws发送消息
                                            JqWebSocket.sendMessage();
                                            return "异常错误";
                                        }
                                    }
                                }
                                //                            ws发送消息
                                JqWebSocket.sendMessage();

//                            System.out.println("最终成功数：" + finRes);
                                System.out.println("list==" + empty);
                            } else {
                                //做最后一个表的备份
                                //最后分完做备份
                                List<String> TabNamels = baseMapper.ShowAllTB(newdataName);

                                for (int i = 0; i < TabNamels.size(); i++) {
                                    mysqlFileName = TabNamels.get(i) + ".sql";
                                    baseMapper.rename(tableName,TabNamels.get(i),newdataName);

                                    mysqlBackupsService.mysqlBackups(Ip,filePath, newdataName, tableName, mysqlFileName, userName, passWord);
                                    //做删除操作
                                    baseMapper.delTB(newdataName,tableName);
                                    System.out.println("备份完毕,删除一个已备份的表");
                                }

                                //判断数据库中是否还有表，没有，则删除库，有则不做处理
                                List<String> allTb = baseMapper.ShowAllTB(newdataName);
                                if (allTb.isEmpty()){
                                    //判断数据库是否有表，如果为空，就删除该数据库
                                    baseMapper.delDataBase(newdataName);
                                }

                                //集合里没有数据了,到最后了，状态为：1.成功
                                //分页无值
                                //0.不删除，1.删除
//                                if (sure == 0) {
//                                    //不删除
//                                    //添加成功记录
//                                    dataSourceTransactionManager.rollback(MonthsStatus);
//                                    recordsheet.setStatus(1);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//
//                                    recordsheet.setSpendtime(spendTime);
//                                    baseMapper.updateById(recordsheet);
//                                    //                                ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    return "成功，不删除原始数据";
//                                } else {
//                                    //删除
//                                    try {
//                                        Boolean aBoolean = delSure(databaseName, tableName, tableIdName);
//                                        if (aBoolean == false) {
//                                            dataSourceTransactionManager.rollback(MonthsStatus);
//                                            //删除数据失败，失败记录
//                                            recordsheet.setStatus(0);
//                                            recordsheet.setEndtime(new Date());
//                                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                            recordsheet.setSpendtime(spendTime);
//                                            baseMapper.updateById(recordsheet);
//                                            //                                ws发送消息
//                                            JqWebSocket.sendMessage();
//                                            return "删除失败";
//                                        }
//                                        recordsheet.setStatus(1);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                        recordsheet.setSpendtime(spendTime);
//                                        baseMapper.updateById(recordsheet);
//                                        dataSourceTransactionManager.commit(MonthsStatus);
//                                        //                                ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "成功添加表并删除了原始数据";
//                                    } catch (Exception n) {
//                                        System.out.println("n的报错" + n.getMessage());
//                                        dataSourceTransactionManager.rollback(MonthsStatus);
//                                        //异常错误，失败记录
//                                        recordsheet.setStatus(0);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                        recordsheet.setSpendtime(spendTime);
//                                        //插入失败的记录
//                                        baseMapper.updateById(recordsheet);
//                                        //                                ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "原始数据删除失败，异常错误";
//                                    }
//                                }
                            }
                            //提交事务
                            dataSourceTransactionManager.commit(MonthsStatus);
                        } catch (Exception e1) {
                            dataSourceTransactionManager.rollback(MonthsStatus);
                            //异常错误，失败记录
                            recordsheet.setStatus(0);
                            recordsheet.setEndtime(new Date());
                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                            recordsheet.setSpendtime(spendTime);
                            //插入失败的记录
                            baseMapper.updateById(recordsheet);
                            //失败后，判断数据库是否存在，存在则插入数据库，不存在则不插入
//                            if (wb == 0) {
//                                datasourceMapper.insert(datasource);
//                            }
                            //                                ws发送消息
                            JqWebSocket.sendMessage();
                            System.out.println("e1的报错" + e1.getMessage());
                            return "添加表失败，请检查填写是否错误";
                        }
                    }
                } else if (type == 2) {
                    //按周分
                    //开始
                    //添加进行中记录

//                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");//设置日期格式
//                    String nowName = df.format(new Date());
                    SimpleDateFormat dbdate = new SimpleDateFormat("yyMMddHHmmss");//设置日期格式
                    String dbstr = dbdate.format(new Date());

                    newdataName = databaseName + "_weeks_"+customField+dbstr;
                    //路径
                    filePath = filePre + newdataName + "/";

                    recordsheet.setNewdataName(newdataName);
                    recordsheet.setStatus(2);
                    recordsheet.setTime(new Date());
//                TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                    int insert = baseMapper.insert(recordsheet);
//                System.out.println("插入状态：" + insert);
                    while (true) {
//                开启事务
                        TransactionStatus WeeksStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                        try {
                            Map<String, List<Long>> TbMap = new HashMap<>();
                            //数据库查询数据
                            List<Long> list = PagData(databaseName, tableName, tableIdName, offset, pageSize);

                            offset += pageSize;
                            //判断是否有数据
                            boolean empty = CollectionUtils.isNotEmpty(list);
                            if (empty) {
                                //分页有值
                                wb = whetherBase(newdataName);

                                if (wb == 0) {
                                    //数据库不存在，则先创建数据库
                                    baseMapper.createLibrary(newdataName);
                                    //随后插入数据库
                                    //设置添加数据库的名字和URL
//                                    datasource.setDsname(newdataName);
//                                    datasource.setUrl(PRE + newdataName + Suffix);
//                                    datasourceMapper.insert(datasource);
                                }
                                //数据库存在，就是wb=1
                                //判断目标表是否存在
                                Integer finRes = 0;

                                for (int i = 0; i < list.size(); i++) {

//                                //2.判断哪一年的：
//                                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                                    //得到当前id对应的时间字段 yyyy年份
                                    Long id = list.get(i);
                                    //得到数据库Date类型时间值
//                                    Date date = baseMapper.findDate(fieldTime, databaseName, tableName, tableIdName, id);
                                    FieldGet fieldGet = baseMapper.fieldGet(fieldTime, customField, databaseName, tableName, tableIdName, id);
                                    Date date = fieldGet.getFieldTime();

                                    int tinyInt = fieldGet.getCustomField();

                                    Calendar calendar = Calendar.getInstance();
                                    calendar.setFirstDayOfWeek(Calendar.MONDAY);
                                    calendar.setTime(date);
                                    int week = calendar.get(Calendar.WEEK_OF_YEAR);

                                    Name = newtableName; // 需要打印中间代理的Name

                                    //3.设置目标表名：格式、例：finance_record_tb week_周
                                    newtableName = tableName + "_week_" + week +"_"+customField+tinyInt;
//                                System.out.println("周表名："+newtableName);

                                    //4.判断目标表是否存在
                                    wt = baseMapper.whetherTable(newdataName, newtableName);
                                    if (wt == 0) {
                                        //表不存在，则先创建表
                                        baseMapper.addTable(newdataName, newtableName, databaseName, tableName);
                                    }

                                    boolean result = newtableName.equals(Name);
//                                System.out.println("前面是否等于后面："+result);
                                    if (result == false && Name != null) {
                                        //如果前面表不等于后面的， 先判断map里有没有这个表key
                                        boolean b = TbMap.containsKey(newtableName);
                                        //如果有
                                        if (b == true) {//添加id到这个表中

                                            TbMap.get(newtableName).add(id);
//                                        System.out.println("有这个key：" + newtableName);
                                        } else {
                                            //如果没有，就new一个list放id 存map
//                                        List<Long> tableIdList= new ArrayList<>();
//                                        tableIdList.add(id);
                                            TbMap.put(newtableName, new ArrayList<>());
                                            TbMap.get(newtableName).add(id);
//                                        System.out.println("没有这个key，这里的name" + Name + "，这里的新表名：" + newtableName);
                                        }
                                    } else {
                                        boolean c = TbMap.containsKey(newtableName);
//                                    System.out.println("是否有"+newtableName+"::"+c);
                                        if (c == false) {
                                            //如果表名相等,直接添加id
                                            TbMap.put(newtableName, new ArrayList<>());
                                            TbMap.get(newtableName).add(id);
                                        } else {
                                            TbMap.get(newtableName).add(id);
                                        }
                                    }
                                }
                                for (Map.Entry<String, List<Long>> entry : TbMap.entrySet()) {
                                    try {
                                        //5.放数据
                                        //得到id
                                        //假装出错 放数据时
//                                int d = 1/0;
//                                    int result = baseMapper.insertData(newdataName, newtableName,databaseName,tableName,tableIdName,id);
//                                    finRes += result ;
//                                    System.out.println("Key = " + entry.getKey());
                                        int result = testInsert(newdataName, entry.getKey(), databaseName, tableName, tableIdName, entry.getValue());
//                                    System.out.println("插入+1");
                                        finRes += result;
                                    } catch (RuntimeException e) {
                                        if (e instanceof DuplicateKeyException) {
                                            System.out.println("该接口数据主键冲突" + e.getMessage());
//                                    dataSourceTransactionManager.rollback(transactionStatus);
                                            //这页的第一个数据主键已存在，则跳过当前页循环，进行下一页数据插入
                                            break;
                                        } else {
                                            dataSourceTransactionManager.rollback(WeeksStatus);
                                            System.out.println("其他错误" + e.getMessage());
                                            //异常错误，失败记录
                                            recordsheet.setStatus(0);
                                            recordsheet.setEndtime(new Date());
                                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                                            recordsheet.setSpendtime(spendTime);
                                            //插入失败的记录
                                            baseMapper.updateById(recordsheet);
                                            //                            ws发送消息
                                            JqWebSocket.sendMessage();
                                            return "异常错误";
                                        }
                                    }
                                }
//                            ws发送消息
                                JqWebSocket.sendMessage();

//                            System.out.println("最终成功数：" + finRes);
                                System.out.println("list==" + empty);
                            } else {
                                //做最后一个表的备份
//                            mysqlFileName = Name + ".sql";
//                            mysqlBackupsService.mysqlBackups(filePath,newdataName,Name,mysqlFileName,userName,passWord);
                                //最后分完做备份
                                List<String> TabNamels = baseMapper.ShowAllTB(newdataName);

                                for (int i = 0; i < TabNamels.size(); i++) {
                                    mysqlFileName = TabNamels.get(i) + ".sql";
                                    baseMapper.rename(tableName,TabNamels.get(i),newdataName);

                                    mysqlBackupsService.mysqlBackups(Ip,filePath, newdataName, tableName, mysqlFileName, userName, passWord);
                                    //做删除操作
                                    baseMapper.delTB(newdataName,tableName);
                                    System.out.println("备份完毕,删除一个已备份的表");
                                }

                                //判断数据库中是否还有表，没有，则删除库，有则不做处理
                                List<String> allTb = baseMapper.ShowAllTB(newdataName);
                                if (allTb.isEmpty()){
                                    //判断数据库是否有表，如果为空，就删除该数据库
                                    baseMapper.delDataBase(newdataName);
                                }

                                //集合里没有数据了,到最后了，状态为：1.成功
                                //分页无值
                                //0.不删除，1.删除
//                                if (sure == 0) {
//                                    //不删除
//                                    //添加成功记录
//                                    dataSourceTransactionManager.rollback(WeeksStatus);
//                                    recordsheet.setStatus(1);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                    recordsheet.setSpendtime(spendTime);
//                                    baseMapper.updateById(recordsheet);
//                                    //                                ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    return "成功，不删除原始数据";
//                                } else {
//                                    //删除
//                                    try {
//                                        Boolean aBoolean = delSure(databaseName, tableName, tableIdName);
//                                        if (aBoolean == false) {
//                                            dataSourceTransactionManager.rollback(WeeksStatus);
//                                            //删除数据失败，失败记录
//                                            recordsheet.setStatus(0);
//                                            recordsheet.setEndtime(new Date());
//                                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                            recordsheet.setSpendtime(spendTime);
//                                            baseMapper.updateById(recordsheet);
//                                            //                                ws发送消息
//                                            JqWebSocket.sendMessage();
//                                            return "删除失败";
//                                        }
//                                        recordsheet.setStatus(1);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                        recordsheet.setSpendtime(spendTime);
//                                        baseMapper.updateById(recordsheet);
//                                        dataSourceTransactionManager.commit(WeeksStatus);
//                                        //                                ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "成功添加表并删除了原始数据";
//
//                                    } catch (Exception n) {
//                                        System.out.println("n的报错" + n.getMessage());
//                                        dataSourceTransactionManager.rollback(WeeksStatus);
//                                        //异常错误，失败记录
//                                        recordsheet.setStatus(0);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                        recordsheet.setSpendtime(spendTime);
//                                        //插入失败的记录
//                                        baseMapper.updateById(recordsheet);
//                                        //                                ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "原始数据删除失败，异常错误";
//                                    }
//                                }
                            }
                            //提交事务
                            dataSourceTransactionManager.commit(WeeksStatus);
                        } catch (Exception e1) {
                            dataSourceTransactionManager.rollback(WeeksStatus);
                            //异常错误，失败记录
                            recordsheet.setStatus(0);
                            recordsheet.setEndtime(new Date());
                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                            recordsheet.setSpendtime(spendTime);
                            //插入失败的记录
                            baseMapper.updateById(recordsheet);
                            //失败后，判断数据库是否存在，存在则插入数据库，不存在则不插入
//                            if (wb == 0) {
//                                datasourceMapper.insert(datasource);
//                            }
                            //                                ws发送消息
                            JqWebSocket.sendMessage();
                            System.out.println("e1的报错" + e1.getMessage());
                            return "添加表失败，请检查填写是否错误";
                        }
                    }
                } else if (type == 3) {
                    //按日分
                    //按年分
                    //开始
                    //添加进行中记录

//                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");//设置日期格式
//                    String nowName = df.format(new Date());
                    SimpleDateFormat dbdate = new SimpleDateFormat("yyMMddHHmmss");//设置日期格式
                    String dbstr = dbdate.format(new Date());

                    newdataName = databaseName + "_day_"+customField+dbstr;
                    //路径
                    filePath = filePre + newdataName + "/";

                    recordsheet.setNewdataName(newdataName);
                    recordsheet.setStatus(2);
                    recordsheet.setTime(new Date());
//                TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                    int insert = baseMapper.insert(recordsheet);
//                System.out.println("插入状态：" + insert);
                    while (true) {
//                开启事务
                        TransactionStatus DaysStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                        try {
                            Map<String, List<Long>> TbMap = new HashMap<>();
                            //数据库查询数据
                            List<Long> list = PagData(databaseName, tableName, tableIdName, offset, pageSize);

                            offset += pageSize;
                            //判断是否有数据
                            boolean empty = CollectionUtils.isNotEmpty(list);
                            if (empty) {
                                //分页有值
                                wb = whetherBase(newdataName);

                                if (wb == 0) {
                                    //数据库不存在，则先创建数据库
                                    baseMapper.createLibrary(newdataName);
                                    //随后插入数据库
                                    //设置添加数据库的名字和URL
//                                    datasource.setDsname(newdataName);
//                                    datasource.setUrl(PRE + newdataName + Suffix);
//                                    datasourceMapper.insert(datasource);
                                }
                                //数据库存在，就是wb=1
                                //判断目标表是否存在
                                Integer finRes = 0;


                                for (int i = 0; i < list.size(); i++) {
                                    //2.判断哪一年的：
                                    SimpleDateFormat format = new SimpleDateFormat("yyyy_MM_dd");
                                    //得到当前id对应的时间字段 yyyy年份
                                    Long id = list.get(i);
                                    //得到数据库Date类型时间值

//                                    Date date = baseMapper.findDate(fieldTime, databaseName, tableName, tableIdName, id);
//                                    String day = format.format(date);
                                    FieldGet fieldGet = baseMapper.fieldGet(fieldTime, customField, databaseName, tableName, tableIdName, id);
                                    Date date = fieldGet.getFieldTime();
                                    String day = format.format(date);

                                    int tinyInt = fieldGet.getCustomField();


                                    Name = newtableName; // 需要打印中间代理的Name

                                    //3.设置目标表名：格式、例：finance_record_tb_天
                                    newtableName = tableName + day +"_"+customField+tinyInt;
                                    //4.判断目标表是否存在
                                    wt = baseMapper.whetherTable(newdataName, newtableName);
                                    if (wt == 0) {
                                        //表不存在，则先创建表
                                        baseMapper.addTable(newdataName, newtableName, databaseName, tableName);
                                    }

                                    boolean result = newtableName.equals(Name);
//                                System.out.println("前面是否等于后面："+result);
                                    if (result == false && Name != null) {
                                        //如果前面表不等于后面的， 先判断map里有没有这个表key
                                        boolean b = TbMap.containsKey(newtableName);
                                        //如果有
                                        if (b == true) {//添加id到这个表中

                                            TbMap.get(newtableName).add(id);
//                                        System.out.println("有这个key：" + newtableName);
                                        } else {
                                            //如果没有，就new一个list放id 存map
//                                        List<Long> tableIdList= new ArrayList<>();
//                                        tableIdList.add(id);
                                            TbMap.put(newtableName, new ArrayList<>());
                                            TbMap.get(newtableName).add(id);
//                                        System.out.println("没有这个key，这里的name" + Name + "，这里的新表名：" + newtableName);
                                        }
                                    } else {
                                        boolean c = TbMap.containsKey(newtableName);
//                                    System.out.println("是否有"+newtableName+"::"+c);
                                        if (c == false) {
                                            //如果表名相等,直接添加id
                                            TbMap.put(newtableName, new ArrayList<>());
                                            TbMap.get(newtableName).add(id);
                                        } else {
                                            TbMap.get(newtableName).add(id);
                                        }
                                    }
                                }
                                for (Map.Entry<String, List<Long>> entry : TbMap.entrySet()) {
                                    try {
                                        //5.放数据
                                        //得到id
                                        //假装出错 放数据时
//                                int d = 1/0;
//                                    int result = baseMapper.insertData(newdataName, newtableName,databaseName,tableName,tableIdName,id);
//                                    finRes += result ;
//                                    System.out.println("Key = " + entry.getKey());
                                        int result = testInsert(newdataName, entry.getKey(), databaseName, tableName, tableIdName, entry.getValue());
//                                    System.out.println("插入+1");
                                        finRes += result;
                                    } catch (RuntimeException e) {
                                        if (e instanceof DuplicateKeyException) {
                                            System.out.println("该接口数据主键冲突" + e.getMessage());
//                                    dataSourceTransactionManager.rollback(transactionStatus);
                                            //这页的第一个数据主键已存在，则跳过当前页循环，进行下一页数据插入
                                            break;
                                        } else {
                                            dataSourceTransactionManager.rollback(DaysStatus);
                                            System.out.println("其他错误" + e.getMessage());
                                            //异常错误，失败记录
                                            recordsheet.setStatus(0);
                                            recordsheet.setEndtime(new Date());
                                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                                            recordsheet.setSpendtime(spendTime);
                                            //插入失败的记录
                                            baseMapper.updateById(recordsheet);
                                            //                            ws发送消息
                                            JqWebSocket.sendMessage();
                                            return "异常错误";
                                        }
                                    }
                                }
                                //                            ws发送消息
                                JqWebSocket.sendMessage();
//                            System.out.println("最终成功数：" + finRes);
                                System.out.println("list==" + empty);
                            } else {
                                //做最后一个表的备份
//                            mysqlFileName = Name + ".sql";
//                            mysqlBackupsService.mysqlBackups(filePath,newdataName,Name,mysqlFileName,userName,passWord);
                                //最后分完做备份
                                List<String> TabNamels = baseMapper.ShowAllTB(newdataName);

                                for (int i = 0; i < TabNamels.size(); i++) {
                                    mysqlFileName = TabNamels.get(i) + ".sql";
                                    baseMapper.rename(tableName,TabNamels.get(i),newdataName);

                                    mysqlBackupsService.mysqlBackups(Ip,filePath, newdataName, tableName, mysqlFileName, userName, passWord);
                                    //做删除操作
                                    baseMapper.delTB(newdataName,tableName);
                                    System.out.println("备份完毕,删除一个已备份的表");
                                }

                                //判断数据库中是否还有表，没有，则删除库，有则不做处理
                                List<String> allTb = baseMapper.ShowAllTB(newdataName);
                                if (allTb.isEmpty()){
                                    //判断数据库是否有表，如果为空，就删除该数据库
                                    baseMapper.delDataBase(newdataName);
                                }

                                //集合里没有数据了,到最后了，状态为：1.成功
                                //分页无值
                                //0.不删除，1.删除
//                                if (sure == 0) {
//                                    //不删除
//                                    //添加成功记录
//                                    dataSourceTransactionManager.rollback(DaysStatus);
//                                    recordsheet.setStatus(1);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                    recordsheet.setSpendtime(spendTime);
//                                    baseMapper.updateById(recordsheet);
//                                    return "成功，不删除原始数据";
//                                } else {
//                                    //删除
//                                    try {
//                                        Boolean aBoolean = delSure(databaseName, tableName, tableIdName);
//                                        if (aBoolean == false) {
//                                            dataSourceTransactionManager.rollback(DaysStatus);
//                                            //删除数据失败，失败记录
//                                            recordsheet.setStatus(0);
//                                            recordsheet.setEndtime(new Date());
//                                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                            recordsheet.setSpendtime(spendTime);
//                                            baseMapper.updateById(recordsheet);
//                                            //                                ws发送消息
//                                            JqWebSocket.sendMessage();
//                                            return "删除失败";
//                                        }
//                                        recordsheet.setStatus(1);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                        recordsheet.setSpendtime(spendTime);
//                                        baseMapper.updateById(recordsheet);
//                                        dataSourceTransactionManager.commit(DaysStatus);
//                                        //                                ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "成功添加表并删除了原始数据";
//                                    } catch (Exception n) {
//                                        System.out.println("n的报错" + n.getMessage());
//                                        dataSourceTransactionManager.rollback(DaysStatus);
//                                        //异常错误，失败记录
//                                        recordsheet.setStatus(0);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                        recordsheet.setSpendtime(spendTime);
//                                        //插入失败的记录
//                                        baseMapper.updateById(recordsheet);
//
//                                        //                                ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "原始数据删除失败，异常错误";
//
//                                    }
//                                }
                            }
                            //提交事务
                            dataSourceTransactionManager.commit(DaysStatus);
                        } catch (Exception e1) {
                            dataSourceTransactionManager.rollback(DaysStatus);
                            //异常错误，失败记录
                            recordsheet.setStatus(0);
                            recordsheet.setEndtime(new Date());
                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                            recordsheet.setSpendtime(spendTime);
                            //插入失败的记录
                            baseMapper.updateById(recordsheet);
                            //失败后，判断数据库是否存在，存在则插入数据库，不存在则不插入
//                            if (wb == 0) {
//                                datasourceMapper.insert(datasource);
//                            }
                            //                                ws发送消息
                            JqWebSocket.sendMessage();
                            System.out.println("e1的报错" + e1.getMessage());
                            return "添加表失败，请检查填写是否错误";
                        }
                    }
                }
            }
        }

        //无分类字段，分类字段为空
        //按时间分
        //自定义时间不为空时
        //====================================================自定义时间字段
        if (customStart !=null && customEnd!=null){
//            System.out.println("自定义时间不为空,结束："+ customEnd);
            if (endType==1){
//                System.out.println("自定义 年");
                //年
//                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");//设置日期格式
//                String nowName = df.format(new Date());
                SimpleDateFormat dbdate = new SimpleDateFormat("yyMMddHHmmss");//设置日期格式
                String dbstr = dbdate.format(new Date());

                newdataName = databaseName + "_cstime"+dbstr;
                //路径
                filePath = filePre + newdataName + "/";

                //数据库存在，就是wb=1
                wb = whetherBase(newdataName);
                if (wb == 0) {
                    //数据库不存在，则先创建数据库
                    baseMapper.createLibrary(newdataName);

                    //随后插入数据库
                    //设置添加数据库的名字和URL
//                    datasource.setDsname(newdataName);
//                    datasource.setUrl(PRE + newdataName + Suffix);
//                    datasourceMapper.insert(datasource);
                }

                //设置添加新库
                recordsheet.setNewdataName(newdataName);
                recordsheet.setStatus(2);
                recordsheet.setTime(new Date());
//                TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                int insert = baseMapper.insert(recordsheet);

//                System.out.println("插入状态：" + insert);
                while (true) {
//                开启事务
                    TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);

                    try {
                        Map<String, List<Long>> TbMap = new HashMap<>();
                        //数据库查询数据
                        List<Long> list = baseMapper.customData(databaseName,tableName,tableIdName,fieldTime,customStart,customEnd,offset,pageSize);
                        //判断是否有数据
                        boolean empty = CollectionUtils.isNotEmpty(list);

                        if (offset==0&&empty==false){
                            dataSourceTransactionManager.rollback(transactionStatus);
                            //删除数据失败，失败记录
                            recordsheet.setStatus(0);
                            recordsheet.setEndtime(new Date());
                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());

                            recordsheet.setSpendtime(spendTime);
                            baseMapper.updateById(recordsheet);
                            //                                ws发送消息
                            JqWebSocket.sendMessage();
                            return "此时间段无数据";
                        }
                        offset += pageSize;
                        if (empty) {
                            //分页有值

                            //判断目标表是否存在
                            Integer finRes = 0;

                            // 一页一页的处理
                            for (int i = 0; i < list.size(); i++) {
                                //2.判断哪一年的：
                                SimpleDateFormat format = new SimpleDateFormat("yyyy");

                                //得到当前id对应的时间字段 yyyy年份
                                Long id = list.get(i);
                                //得到数据库Date类型时间值

                                Date date = baseMapper.findDate(fieldTime, databaseName, tableName, tableIdName, id);
                                String year = format.format(date);

                                Name = newtableName; // 需要打印中间代理的Name

                                //3.设置目标表名：格式、例：finance_record_tb年
                                newtableName = tableName + "_custom" + year;


                                //4.判断目标表是否存在
                                wt = baseMapper.whetherTable(newdataName, newtableName);
                                if (wt == 0) {
                                    //表不存在，则先创建表
                                    baseMapper.addTable(newdataName, newtableName, databaseName, tableName);
                                }

                                boolean result = newtableName.equals(Name);
//                                System.out.println("前面是否等于后面："+result);
                                if (result == false && Name != null) {
                                    //如果前面表不等于后面的， 先判断map里有没有这个表key
                                    boolean b = TbMap.containsKey(newtableName);
                                    //如果有
                                    if (b == true) {//添加id到这个表中

                                        TbMap.get(newtableName).add(id);
//                                        System.out.println("有这个key："+newtableName);
                                    } else {
                                        //如果没有，就new一个list放id 存map
//                                        List<Long> tableIdList= new ArrayList<>();
//                                        tableIdList.add(id);
                                        TbMap.put(newtableName, new ArrayList<>());
                                        TbMap.get(newtableName).add(id);
//                                        System.out.println("没有这个key，这里的name"+Name+"，这里的新表名："+newtableName);
                                    }
                                } else {
                                    boolean c = TbMap.containsKey(newtableName);
//                                    System.out.println("是否有"+newtableName+"::"+c);
                                    if (c == false) {
                                        //如果表名相等,直接添加id
                                        TbMap.put(newtableName, new ArrayList<>());
                                        TbMap.get(newtableName).add(id);
                                    } else {
                                        TbMap.get(newtableName).add(id);
                                    }
                                }
                            }
                            for (Map.Entry<String, List<Long>> entry : TbMap.entrySet()) {
                                try {
                                    //5.放数据
                                    int result = testInsert(newdataName, entry.getKey(), databaseName, tableName, tableIdName, entry.getValue());
                                    finRes += result;
                                } catch (RuntimeException e) {
                                    if (e instanceof DuplicateKeyException) {
                                        System.out.println("该接口数据主键冲突" + e.getMessage());
                                        //这页的第一个数据主键已存在，则跳过当前页循环，进行下一页数据插入
                                        break;
                                    } else {
                                        dataSourceTransactionManager.rollback(transactionStatus);
                                        System.out.println("其他错误" + e.getMessage());
                                        //异常错误，失败记录
                                        recordsheet.setStatus(0);
                                        recordsheet.setEndtime(new Date());
                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                                        recordsheet.setSpendtime(spendTime);
                                        //插入失败的记录
                                        baseMapper.updateById(recordsheet);
                                        //                            ws发送消息
                                        JqWebSocket.sendMessage();
                                        return "异常错误";
                                    }
                                }
                            }
//                            ws发送消息
                            JqWebSocket.sendMessage();

                            System.out.println("list==" + empty);
                        } else {
                            //做最后一个表的备份
                            //最后分完做备份
                            List<String> TabNamels = baseMapper.ShowAllTB(newdataName);

                            for (int i = 0; i < TabNamels.size(); i++) {
                                mysqlFileName = TabNamels.get(i) + ".sql";
                                baseMapper.rename(tableName,TabNamels.get(i),newdataName);

                                mysqlBackupsService.mysqlBackups(Ip,filePath, newdataName, tableName, mysqlFileName, userName, passWord);
                                //做删除操作
                                baseMapper.delTB(newdataName,tableName);
                                System.out.println("备份完毕,删除一个已备份的表");
                            }

                            //判断数据库中是否还有表，没有，则删除库，有则不做处理
                            List<String> allTb = baseMapper.ShowAllTB(newdataName);
                            if (allTb.isEmpty()){
                                //判断数据库是否有表，如果为空，就删除该数据库
                                baseMapper.delDataBase(newdataName);
                            }

                            //集合里没有数据了,到最后了，状态为：1.成功
                            //分页无值
                            //0.不删除，1.删除
//                            if (sure == 0) {
//                                //不删除
//                                //添加成功记录
//                                dataSourceTransactionManager.rollback(transactionStatus);
//                                //结束，更改记录数据
//                                recordsheet.setStatus(1);
//                                recordsheet.setEndtime(new Date());
//                                String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                recordsheet.setSpendtime(spendTime);
//                                baseMapper.updateById(recordsheet);
////                                ws发送消息
//                                JqWebSocket.sendMessage();
//                                return "成功，不删除原始数据";
//                            } else {
//                                //自定义时间的删除
//                                try {
//                                    List<Long> allIdList = baseMapper.allCustomId(tableIdName, databaseName, tableName, fieldTime, customStart, customEnd);
//
//                                    int i = delCustomSure(databaseName, tableName, tableIdName, allIdList);
//
//                                    if (i == 0) {
//                                        //没有删除数据
//                                            dataSourceTransactionManager.rollback(transactionStatus);
//                                            //删除数据失败，失败记录
//                                            recordsheet.setStatus(0);
//                                            recordsheet.setEndtime(new Date());
//                                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//
//                                            recordsheet.setSpendtime(spendTime);
//                                            baseMapper.updateById(recordsheet);
//                                            //                                ws发送消息
//                                            JqWebSocket.sendMessage();
//                                            return "删除失败";
//                                        }
//
//                                    recordsheet.setStatus(1);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                    recordsheet.setSpendtime(spendTime);
//                                    baseMapper.updateById(recordsheet);
//                                    dataSourceTransactionManager.commit(transactionStatus);
//                                    //  ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    return "成功添加表并删除了原始数据";
//                                } catch (Exception n) {
//                                    System.out.println("n的报错" + n.getMessage());
//                                    dataSourceTransactionManager.rollback(transactionStatus);
//                                    //异常错误，失败记录
//                                    recordsheet.setStatus(0);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//
//                                    recordsheet.setSpendtime(spendTime);
//                                    //插入失败的记录
//                                    baseMapper.updateById(recordsheet);
//                                    //                                ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    return "原始数据删除失败，异常错误";
//                                }
//                            }
                        }
                        //提交事务
                        dataSourceTransactionManager.commit(transactionStatus);

                    } catch (Exception e1) {
                        dataSourceTransactionManager.rollback(transactionStatus);
                        //异常错误，失败记录
                        recordsheet.setStatus(0);
                        recordsheet.setEndtime(new Date());
                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                        recordsheet.setSpendtime(spendTime);
                        //插入失败的记录
                        baseMapper.updateById(recordsheet);
                        //失败后，判断数据库是否存在，存在则插入数据库，不存在则不插入
//                        if (wb == 0) {
//                            datasourceMapper.insert(datasource);
//                        }
                        //                                ws发送消息
                        JqWebSocket.sendMessage();
                        System.out.println("e1的报错" + e1.getMessage());
                        return "添加表失败，请检查填写是否错误";
                    }
                }
            }else if (endType==2){
                //月
//                System.out.println("自定义 月");

//                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");//设置日期格式
//                String nowName = df.format(new Date());
                SimpleDateFormat dbdate = new SimpleDateFormat("yyMMddHHmmss");//设置日期格式
                String dbstr = dbdate.format(new Date());

                newdataName = databaseName + "_cstime"+dbstr;
                //路径
                filePath = filePre + newdataName + "/";

                //判断数据库是否存在
                wb = whetherBase(newdataName);

                //数据库存在，就是wb=1
                if (wb == 0) {
                    System.out.println("数据库不存在"+newdataName);
                    //数据库不存在，则先创建数据库
                    baseMapper.createLibrary(newdataName);
                    //随后插入数据库
                    //设置添加数据库的名字和URL
//                    datasource.setDsname(newdataName);
//                    datasource.setUrl(PRE + newdataName + Suffix);
//                    int insert = datasourceMapper.insert(datasource);
//                    System.out.println("数据库插入状态：" + insert);
                }

                recordsheet.setNewdataName(newdataName);
                recordsheet.setStatus(2);
                recordsheet.setTime(new Date());
//                TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                int insert = baseMapper.insert(recordsheet);
                System.out.println("插入状态：" + insert);
                while (true) {
//                开启事务
                    TransactionStatus MonthsStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                    try {
                        Map<String, List<Long>> TbMap = new HashMap<>();
                        //数据库查询数据
                        List<Long> list = baseMapper.customData(databaseName,tableName,tableIdName,fieldTime,customStart,customEnd,offset,pageSize);
                        //判断是否有数据
                        boolean empty = CollectionUtils.isNotEmpty(list);
//                        System.out.println("判断是否有数据："+empty);
                        if (offset==0&&empty==false){
                            dataSourceTransactionManager.rollback(MonthsStatus);
                            //删除数据失败，失败记录
                            recordsheet.setStatus(0);
                            recordsheet.setEndtime(new Date());
                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                            recordsheet.setSpendtime(spendTime);
                            baseMapper.updateById(recordsheet);
                            //ws发送消息
                            JqWebSocket.sendMessage();
                            return "此时间段无数据";
                        }
                        offset += pageSize;
                        if (empty) {
                            //分页有值
                            //判断目标表是否存在
                            Integer finRes = 0;
                            for (int i = 0; i < list.size(); i++) {
                                //2.判断哪一年的：
                                SimpleDateFormat format = new SimpleDateFormat("yyyy_MM");
                                //得到当前id对应的时间字段 yyyy_MM
                                Long id = list.get(i);
                                //得到数据库Date类型时间值

                                Date date = baseMapper.findDate(fieldTime, databaseName, tableName, tableIdName, id);
                                String year_month = format.format(date);
                                Name = newtableName; // 需要打印中间代理的Name

                                //3.设置目标表名：格式、例：finance_record_tb年月
                                newtableName = tableName + "_custom" + year_month;
                                //4.判断目标表是否存在
                                wt = baseMapper.whetherTable(newdataName, newtableName);
                                if (wt == 0) {
                                    //表不存在，则先创建表
                                    baseMapper.addTable(newdataName, newtableName, databaseName, tableName);
                                }
                                boolean result = newtableName.equals(Name);
                                if (result == false && Name != null) {
                                    //如果前面表不等于后面的， 先判断map里有没有这个表key
                                    boolean b = TbMap.containsKey(newtableName);
                                    //如果有
                                    if (b == true) {//添加id到这个表中
                                        TbMap.get(newtableName).add(id);
//                                        System.out.println("有这个key：" + newtableName);
                                    } else {
                                        //如果没有，就new一个list放id 存map
//                                        List<Long> tableIdList= new ArrayList<>();
//                                        tableIdList.add(id);
                                        TbMap.put(newtableName, new ArrayList<>());
                                        TbMap.get(newtableName).add(id);
//                                        System.out.println("没有这个key，这里的name" + Name + "，这里的新表名：" + newtableName);
                                    }
                                } else {
                                    boolean c = TbMap.containsKey(newtableName);
//                                    System.out.println("是否有"+newtableName+"::"+c);
                                    if (c == false) {
                                        //如果表名相等,直接添加id
                                        TbMap.put(newtableName, new ArrayList<>());
                                        TbMap.get(newtableName).add(id);
                                    } else {
                                        TbMap.get(newtableName).add(id);
                                    }
                                }
                            }
                            for (Map.Entry<String, List<Long>> entry : TbMap.entrySet()) {
                                try {
                                    //5.放数据
                                    int result = testInsert(newdataName, entry.getKey(), databaseName, tableName, tableIdName, entry.getValue());
                                    finRes += result;
                                } catch (RuntimeException e) {
                                    if (e instanceof DuplicateKeyException) {
                                        System.out.println("该接口数据主键冲突" + e.getMessage());
//                                    dataSourceTransactionManager.rollback(transactionStatus);
                                        //这页的第一个数据主键已存在，则跳过当前页循环，进行下一页数据插入
                                        break;
                                    } else {
                                        dataSourceTransactionManager.rollback(MonthsStatus);
                                        System.out.println("其他错误" + e.getMessage());
                                        //异常错误，失败记录
                                        recordsheet.setStatus(0);
                                        recordsheet.setEndtime(new Date());
                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                                        recordsheet.setSpendtime(spendTime);
                                        //插入失败的记录
                                        baseMapper.updateById(recordsheet);
                                        //                            ws发送消息
                                        JqWebSocket.sendMessage();
                                        return "异常错误";
                                    }
                                }
                            }
                            //                            ws发送消息
                            JqWebSocket.sendMessage();

//                            System.out.println("最终成功数：" + finRes);
                            System.out.println("list==" + empty);
                        } else {
                            //做最后一个表的备份
                            //最后分完做备份
//                            System.out.println("到这里====");
                            List<String> TabNamels = baseMapper.ShowAllTB(newdataName);
//                            System.out.println("出错");
                            for (int i = 0; i < TabNamels.size(); i++) {
                                mysqlFileName = TabNamels.get(i) + ".sql";
                                baseMapper.rename(tableName,TabNamels.get(i),newdataName);

                                mysqlBackupsService.mysqlBackups(Ip,filePath, newdataName, tableName, mysqlFileName, userName, passWord);
                                //做删除操作
                                baseMapper.delTB(newdataName,tableName);
                                System.out.println("备份完毕,删除一个已备份的表");
                            }

                            //判断数据库中是否还有表，没有，则删除库，有则不做处理
                            List<String> allTb = baseMapper.ShowAllTB(newdataName);
                            if (allTb.isEmpty()){
                                //判断数据库是否有表，如果为空，就删除该数据库
                                baseMapper.delDataBase(newdataName);
                            }

                            //集合里没有数据了,到最后了，状态为：1.成功
                            //分页无值
                            //0.不删除，1.删除
//                            if (sure == 0) {
//                                //不删除
//                                //添加成功记录
//                                dataSourceTransactionManager.rollback(MonthsStatus);
//                                recordsheet.setStatus(1);
//                                recordsheet.setEndtime(new Date());
//                                String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//
//                                recordsheet.setSpendtime(spendTime);
//                                baseMapper.updateById(recordsheet);
//                                //                                ws发送消息
//                                JqWebSocket.sendMessage();
//                                return "成功，不删除原始数据";
//                            } else {
//                                //删除
//                                try {
//                                    List<Long> allIdList = baseMapper.allCustomId(tableIdName, databaseName, tableName, fieldTime, customStart, customEnd);
//
//                                    int i = delCustomSure(databaseName, tableName, tableIdName, allIdList);
//
//                                    if (i == 0) {
//                                        //没有删除数据
//                                        dataSourceTransactionManager.rollback(MonthsStatus);
//                                        //删除数据失败，失败记录
//                                        recordsheet.setStatus(0);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//
//                                        recordsheet.setSpendtime(spendTime);
//                                        baseMapper.updateById(recordsheet);
//                                        //                                ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "删除失败";
//                                    }
//
//                                    recordsheet.setStatus(1);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                    recordsheet.setSpendtime(spendTime);
//                                    baseMapper.updateById(recordsheet);
//                                    dataSourceTransactionManager.commit(MonthsStatus);
//                                    //                                ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    return "成功添加表并删除了原始数据";
//                                } catch (Exception n) {
//                                    System.out.println("n的报错" + n.getMessage());
//                                    dataSourceTransactionManager.rollback(MonthsStatus);
//                                    //异常错误，失败记录
//                                    recordsheet.setStatus(0);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                    recordsheet.setSpendtime(spendTime);
//                                    //插入失败的记录
//                                    baseMapper.updateById(recordsheet);
//                                    //                                ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    return "原始数据删除失败，异常错误";
//                                }
//                            }
                        }
                        //提交事务
                        dataSourceTransactionManager.commit(MonthsStatus);
                    } catch (Exception e1) {
                        dataSourceTransactionManager.rollback(MonthsStatus);
                        //异常错误，失败记录
                        recordsheet.setStatus(0);
                        recordsheet.setEndtime(new Date());
                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                        recordsheet.setSpendtime(spendTime);
                        //插入失败的记录
                        baseMapper.updateById(recordsheet);
                        //失败后，判断数据库是否存在，存在则插入数据库，不存在则不插入
//                        if (wb == 0) {
//                            datasourceMapper.insert(datasource);
//                        }
                        //                                ws发送消息
                        JqWebSocket.sendMessage();
                        System.out.println("e1的报错" + e1.getMessage());
                        return "添加表失败，请检查填写是否错误";
                    }
                }

            }else if (endType==3){
//                System.out.println("开始时间"+customStart);
//                System.out.println("自定义结束时间"+customEnd);
                //周
//                System.out.println("自定义 周");

//                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");//设置日期格式
//                String nowName = df.format(new Date());
                SimpleDateFormat dbdate = new SimpleDateFormat("yyMMddHHmmss");//设置日期格式
                String dbstr = dbdate.format(new Date());

                newdataName = databaseName + "_cstime"+dbstr;
                //路径
                filePath = filePre + newdataName + "/";

                wb = whetherBase(newdataName);

                //数据库存在，就是wb=1
                if (wb == 0) {
                    //数据库不存在，则先创建数据库
                    baseMapper.createLibrary(newdataName);
                    //随后插入数据库
                    //设置添加数据库的名字和URL
//                    datasource.setDsname(newdataName);
//                    datasource.setUrl(PRE + newdataName + Suffix);
//                    datasourceMapper.insert(datasource);
                }

                recordsheet.setNewdataName(newdataName);
                recordsheet.setStatus(2);
                recordsheet.setTime(new Date());
//                TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                int insert = baseMapper.insert(recordsheet);
//                System.out.println("插入状态：" + insert);
                while (true) {
//                开启事务
                    TransactionStatus WeeksStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                    try {
                        Map<String, List<Long>> TbMap = new HashMap<>();
                        //数据库查询数据
                        List<Long> list = baseMapper.customData(databaseName,tableName,tableIdName,fieldTime,customStart,customEnd,offset,pageSize);

                        //判断是否有数据
                        boolean empty = CollectionUtils.isNotEmpty(list);
                        if (offset==0&&empty==false){
                            dataSourceTransactionManager.rollback(WeeksStatus);
                            //异常错误，失败记录
                            recordsheet.setStatus(0);
                            recordsheet.setEndtime(new Date());
                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                            recordsheet.setSpendtime(spendTime);
                            //插入失败的记录
                            baseMapper.updateById(recordsheet);
                            //                            ws发送消息
                            JqWebSocket.sendMessage();
                            return "此时间段无数据";
                        }
                        offset += pageSize;

                        if (empty) {
                            //分页有值
                            //判断目标表是否存在
                            Integer finRes = 0;

                            for (int i = 0; i < list.size(); i++) {

//                                //2.判断哪一年的：
//                                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                                //得到当前id对应的时间字段 yyyy年份
                                Long id = list.get(i);
                                //得到数据库Date类型时间值
                                Date date = baseMapper.findDate(fieldTime, databaseName, tableName, tableIdName, id);
                                Calendar calendar = Calendar.getInstance();
                                calendar.setFirstDayOfWeek(Calendar.MONDAY);
                                calendar.setTime(date);
                                int week = calendar.get(Calendar.WEEK_OF_YEAR);

                                Name = newtableName; // 需要打印中间代理的Name

                                //3.设置目标表名：格式、例：finance_record_tb week_周
                                newtableName = tableName + "_" + "custom_week_" + week;
//                                System.out.println("周表名："+newtableName);

                                //4.判断目标表是否存在
                                wt = baseMapper.whetherTable(newdataName, newtableName);
                                if (wt == 0) {
                                    //表不存在，则先创建表
                                    baseMapper.addTable(newdataName, newtableName, databaseName, tableName);
                                }

                                boolean result = newtableName.equals(Name);
//                                System.out.println("前面是否等于后面："+result);
                                if (result == false && Name != null) {
                                    //如果前面表不等于后面的， 先判断map里有没有这个表key
                                    boolean b = TbMap.containsKey(newtableName);
                                    //如果有
                                    if (b == true) {//添加id到这个表中

                                        TbMap.get(newtableName).add(id);
//                                        System.out.println("有这个key：" + newtableName);
                                    } else {
                                        //如果没有，就new一个list放id 存map
//                                        List<Long> tableIdList= new ArrayList<>();
//                                        tableIdList.add(id);
                                        TbMap.put(newtableName, new ArrayList<>());
                                        TbMap.get(newtableName).add(id);
//                                        System.out.println("没有这个key，这里的name" + Name + "，这里的新表名：" + newtableName);
                                    }
                                } else {
                                    boolean c = TbMap.containsKey(newtableName);
//                                    System.out.println("是否有"+newtableName+"::"+c);
                                    if (c == false) {
                                        //如果表名相等,直接添加id
                                        TbMap.put(newtableName, new ArrayList<>());
                                        TbMap.get(newtableName).add(id);
                                    } else {
                                        TbMap.get(newtableName).add(id);
                                    }
                                }
                            }
                            for (Map.Entry<String, List<Long>> entry : TbMap.entrySet()) {
                                try {
                                    //5.放数据
                                    int result = testInsert(newdataName, entry.getKey(), databaseName, tableName, tableIdName, entry.getValue());
                                    finRes += result;
                                } catch (RuntimeException e) {
                                    if (e instanceof DuplicateKeyException) {
                                        System.out.println("该接口数据主键冲突" + e.getMessage());
//                                    dataSourceTransactionManager.rollback(transactionStatus);
                                        //这页的第一个数据主键已存在，则跳过当前页循环，进行下一页数据插入
                                        break;
                                    } else {
                                        dataSourceTransactionManager.rollback(WeeksStatus);
                                        System.out.println("其他错误" + e.getMessage());
                                        //异常错误，失败记录
                                        recordsheet.setStatus(0);
                                        recordsheet.setEndtime(new Date());
                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                                        recordsheet.setSpendtime(spendTime);
                                        //插入失败的记录
                                        baseMapper.updateById(recordsheet);
                                        //                            ws发送消息
                                        JqWebSocket.sendMessage();
                                        return "异常错误";
                                    }
                                }
                            }
//                            ws发送消息
                            JqWebSocket.sendMessage();

//                            System.out.println("最终成功数：" + finRes);
                            System.out.println("list==" + empty);
                        } else {
                            //做最后一个表的备份
//                            mysqlFileName = Name + ".sql";
//                            mysqlBackupsService.mysqlBackups(filePath,newdataName,Name,mysqlFileName,userName,passWord);
                            //最后分完做备份
                            List<String> TabNamels = baseMapper.ShowAllTB(newdataName);

                            for (int i = 0; i < TabNamels.size(); i++) {
                                mysqlFileName = TabNamels.get(i) + ".sql";
                                baseMapper.rename(tableName,TabNamels.get(i),newdataName);

                                mysqlBackupsService.mysqlBackups(Ip,filePath, newdataName, tableName, mysqlFileName, userName, passWord);
                                //做删除操作
                                baseMapper.delTB(newdataName,tableName);
                                System.out.println("备份完毕,删除一个已备份的表");
                            }

                            //判断数据库中是否还有表，没有，则删除库，有则不做处理
                            List<String> allTb = baseMapper.ShowAllTB(newdataName);
                            if (allTb.isEmpty()){
                                //判断数据库是否有表，如果为空，就删除该数据库
                                baseMapper.delDataBase(newdataName);
                            }

                            //集合里没有数据了,到最后了，状态为：1.成功
                            //分页无值
                            //0.不删除，1.删除
//                            if (sure == 0) {
//                                //不删除
//                                //添加成功记录
//                                dataSourceTransactionManager.rollback(WeeksStatus);
//                                recordsheet.setStatus(1);
//                                recordsheet.setEndtime(new Date());
//                                String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                recordsheet.setSpendtime(spendTime);
//                                baseMapper.updateById(recordsheet);
//                                //                                ws发送消息
//                                JqWebSocket.sendMessage();
//                                return "成功，不删除原始数据";
//                            } else {
//                                //删除
//                                try {
//                                    List<Long> allIdList = baseMapper.allCustomId(tableIdName, databaseName, tableName, fieldTime, customStart, customEnd);
//
//                                    int i = delCustomSure(databaseName, tableName, tableIdName, allIdList);
//
//                                    if (i == 0) {
//                                        //没有删除数据
//                                        dataSourceTransactionManager.rollback(WeeksStatus);
//                                        //删除数据失败，失败记录
//                                        recordsheet.setStatus(0);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//
//                                        recordsheet.setSpendtime(spendTime);
//                                        baseMapper.updateById(recordsheet);
//                                        //                                ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "删除失败";
//                                    }
//
//                                    recordsheet.setStatus(1);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                    recordsheet.setSpendtime(spendTime);
//                                    baseMapper.updateById(recordsheet);
//                                    dataSourceTransactionManager.commit(WeeksStatus);
//                                    //                                ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    return "成功添加表并删除了原始数据";
//
//                                } catch (Exception n) {
//                                    System.out.println("n的报错" + n.getMessage());
//                                    dataSourceTransactionManager.rollback(WeeksStatus);
//                                    //异常错误，失败记录
//                                    recordsheet.setStatus(0);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                    recordsheet.setSpendtime(spendTime);
//                                    //插入失败的记录
//                                    baseMapper.updateById(recordsheet);
//                                    //                                ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    return "原始数据删除失败，异常错误";
//                                }
//                            }
                        }
                        //提交事务
                        dataSourceTransactionManager.commit(WeeksStatus);
                    } catch (Exception e1) {
                        dataSourceTransactionManager.rollback(WeeksStatus);
                        //异常错误，失败记录
                        recordsheet.setStatus(0);
                        recordsheet.setEndtime(new Date());
                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                        recordsheet.setSpendtime(spendTime);
                        //插入失败的记录
                        baseMapper.updateById(recordsheet);
                        //失败后，判断数据库是否存在，存在则插入数据库，不存在则不插入
//                        if (wb == 0) {
//                            datasourceMapper.insert(datasource);
//                        }
                        //                                ws发送消息
                        JqWebSocket.sendMessage();
                        System.out.println("e1的报错" + e1.getMessage());
                        return "添加表失败，请检查填写是否错误";
                    }
                }
            }else if (endType==4){
                //日
//                System.out.println("自定义 日");


//                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");//设置日期格式
//                String nowName = df.format(new Date());
                SimpleDateFormat dbdate = new SimpleDateFormat("yyMMddHHmmss");//设置日期格式
                String dbstr = dbdate.format(new Date());

                newdataName = databaseName + "_cstime"+dbstr;
                //路径
                filePath = filePre + newdataName + "/";

                wb = whetherBase(newdataName);

                //数据库存在，就是wb=1
                if (wb == 0) {
                    //数据库不存在，则先创建数据库
                    baseMapper.createLibrary(newdataName);
                    //随后插入数据库
                    //设置添加数据库的名字和URL
//                    datasource.setDsname(newdataName);
//                    datasource.setUrl(PRE + newdataName + Suffix);
//                    datasourceMapper.insert(datasource);
                }

                recordsheet.setNewdataName(newdataName);
                recordsheet.setStatus(2);
                recordsheet.setTime(new Date());
//                TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                int insert = baseMapper.insert(recordsheet);
//                System.out.println("插入状态：" + insert);
                while (true) {
//                开启事务
                    TransactionStatus DaysStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                    try {
                        Map<String, List<Long>> TbMap = new HashMap<>();
                        //数据库查询数据
                        List<Long> list = baseMapper.customData(databaseName, tableName, tableIdName, fieldTime, customStart, customEnd, offset, pageSize);

                        //判断是否有数据
                        boolean empty = CollectionUtils.isNotEmpty(list);
                        if (offset==0&&empty==false){
                            dataSourceTransactionManager.rollback(DaysStatus);
                            //删除数据失败，失败记录
                            recordsheet.setStatus(0);
                            recordsheet.setEndtime(new Date());
                            String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                            recordsheet.setSpendtime(spendTime);
                            baseMapper.updateById(recordsheet);
                            //                                ws发送消息
                            JqWebSocket.sendMessage();
                            return "此时间段无数据";
                        }
                        offset += pageSize;

                        if (empty) {
                            //分页有值
                            //判断目标表是否存在
                            Integer finRes = 0;

                            for (int i = 0; i < list.size(); i++) {
                                //2.判断哪一年的：
                                SimpleDateFormat format = new SimpleDateFormat("yyyy_MM_dd");
                                //得到当前id对应的时间字段 yyyy年份
                                Long id = list.get(i);
                                //得到数据库Date类型时间值

                                Date date = baseMapper.findDate(fieldTime, databaseName, tableName, tableIdName, id);
                                String day = format.format(date);
                                Name = newtableName; // 需要打印中间代理的Name

                                //3.设置目标表名：格式、例：finance_record_tb_天
                                newtableName = tableName + "_custom" + day;
                                //4.判断目标表是否存在
                                wt = baseMapper.whetherTable(newdataName, newtableName);
                                if (wt == 0) {
                                    //表不存在，则先创建表
                                    baseMapper.addTable(newdataName, newtableName, databaseName, tableName);
                                }

                                boolean result = newtableName.equals(Name);
//                                System.out.println("前面是否等于后面："+result);
                                if (result == false && Name != null) {
                                    //如果前面表不等于后面的， 先判断map里有没有这个表key
                                    boolean b = TbMap.containsKey(newtableName);
                                    //如果有
                                    if (b == true) {//添加id到这个表中

                                        TbMap.get(newtableName).add(id);
//                                        System.out.println("有这个key：" + newtableName);
                                    } else {
                                        //如果没有，就new一个list放id 存map
//                                        List<Long> tableIdList= new ArrayList<>();
//                                        tableIdList.add(id);
                                        TbMap.put(newtableName, new ArrayList<>());
                                        TbMap.get(newtableName).add(id);
//                                        System.out.println("没有这个key，这里的name" + Name + "，这里的新表名：" + newtableName);
                                    }
                                } else {
                                    boolean c = TbMap.containsKey(newtableName);
//                                    System.out.println("是否有"+newtableName+"::"+c);
                                    if (c == false) {
                                        //如果表名相等,直接添加id
                                        TbMap.put(newtableName, new ArrayList<>());
                                        TbMap.get(newtableName).add(id);
                                    } else {
                                        TbMap.get(newtableName).add(id);
                                    }
                                }
                            }
                            for (Map.Entry<String, List<Long>> entry : TbMap.entrySet()) {
                                try {
                                    //5.放数据
                                    int result = testInsert(newdataName, entry.getKey(), databaseName, tableName, tableIdName, entry.getValue());
                                    finRes += result;
                                } catch (RuntimeException e) {
                                    if (e instanceof DuplicateKeyException) {
                                        System.out.println("该接口数据主键冲突" + e.getMessage());
//                                    dataSourceTransactionManager.rollback(transactionStatus);
                                        //这页的第一个数据主键已存在，则跳过当前页循环，进行下一页数据插入
                                        break;
                                    } else {
                                        dataSourceTransactionManager.rollback(DaysStatus);
                                        System.out.println("其他错误" + e.getMessage());
                                        //异常错误，失败记录
                                        recordsheet.setStatus(0);
                                        recordsheet.setEndtime(new Date());
                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                                        recordsheet.setSpendtime(spendTime);
                                        //插入失败的记录
                                        baseMapper.updateById(recordsheet);
                                        //                            ws发送消息
                                        JqWebSocket.sendMessage();
                                        return "异常错误";
                                    }
                                }
                            }
                            //                            ws发送消息
                            JqWebSocket.sendMessage();

//                            System.out.println("最终成功数：" + finRes);
                            System.out.println("list==" + empty);
                        } else {
                            //做最后一个表的备份
//                            mysqlFileName = Name + ".sql";
//                            mysqlBackupsService.mysqlBackups(filePath,newdataName,Name,mysqlFileName,userName,passWord);
                            //最后分完做备份
                            List<String> TabNamels = baseMapper.ShowAllTB(newdataName);

                            for (int i = 0; i < TabNamels.size(); i++) {
                                mysqlFileName = TabNamels.get(i) + ".sql";
                                baseMapper.rename(tableName,TabNamels.get(i),newdataName);

                                mysqlBackupsService.mysqlBackups(Ip,filePath, newdataName, tableName, mysqlFileName, userName, passWord);
                                //做删除操作
                                baseMapper.delTB(newdataName,tableName);
                                System.out.println("备份完毕,删除一个已备份的表");
                            }

                            //判断数据库中是否还有表，没有，则删除库，有则不做处理
                            List<String> allTb = baseMapper.ShowAllTB(newdataName);
                            if (allTb.isEmpty()){
                                //判断数据库是否有表，如果为空，就删除该数据库
                                baseMapper.delDataBase(newdataName);
                            }

                            //集合里没有数据了,到最后了，状态为：1.成功
                            //分页无值
                            //0.不删除，1.删除
//                            if (sure == 0) {
//                                //不删除
//                                //添加成功记录
//                                dataSourceTransactionManager.rollback(DaysStatus);
//                                recordsheet.setStatus(1);
//                                recordsheet.setEndtime(new Date());
//                                String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                recordsheet.setSpendtime(spendTime);
//                                baseMapper.updateById(recordsheet);
//                                return "成功，不删除原始数据";
//                            } else {
//                                //删除
//                                try {
//
//                                    List<Long> allIdList = baseMapper.allCustomId(tableIdName, databaseName, tableName, fieldTime, customStart, customEnd);
//
//                                    int i = delCustomSure(databaseName, tableName, tableIdName, allIdList);
//
//                                    if (i == 0) {
//                                        //没有删除数据
//                                        dataSourceTransactionManager.rollback(DaysStatus);
//                                        //删除数据失败，失败记录
//                                        recordsheet.setStatus(0);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//
//                                        recordsheet.setSpendtime(spendTime);
//                                        baseMapper.updateById(recordsheet);
//                                        //                                ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "删除失败";
//                                    }
//
//                                    recordsheet.setStatus(1);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                    recordsheet.setSpendtime(spendTime);
//                                    baseMapper.updateById(recordsheet);
//                                    dataSourceTransactionManager.commit(DaysStatus);
//                                    //                                ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    return "成功添加表并删除了原始数据";
//
//                                } catch (Exception n) {
//                                    System.out.println("n的报错" + n.getMessage());
//                                    dataSourceTransactionManager.rollback(DaysStatus);
//                                    //异常错误，失败记录
//                                    recordsheet.setStatus(0);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                    recordsheet.setSpendtime(spendTime);
//                                    //插入失败的记录
//                                    baseMapper.updateById(recordsheet);
//                                    //                                ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    return "原始数据删除失败，异常错误";
//                                }
//                            }
                        }
                        //提交事务
                        dataSourceTransactionManager.commit(DaysStatus);
                    } catch (Exception e1) {
                        dataSourceTransactionManager.rollback(DaysStatus);
                        //异常错误，失败记录
                        recordsheet.setStatus(0);
                        recordsheet.setEndtime(new Date());
                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                        recordsheet.setSpendtime(spendTime);
                        //插入失败的记录
                        baseMapper.updateById(recordsheet);
                        //失败后，判断数据库是否存在，存在则插入数据库，不存在则不插入
//                        if (wb == 0) {
//                            datasourceMapper.insert(datasource);
//                        }
                        //                                ws发送消息
                        JqWebSocket.sendMessage();
                        System.out.println("e1的报错" + e1.getMessage());
                        return "添加表失败，请检查填写是否错误";
                    }
                }
            }
        } else {
            //自定义为空，type也为空时
            if (type == null) {
                return "请选择模式";
            }
            //================================================type时间字段
            //自定义时间为空时
            //type=0年，type=1月，
            if (type == 0) {
                //1.查询1000条，判断是哪一年的，放入对应的年表，存在就直接放，不存在，创建数据库，在放。(时间段在当年)
                //2.分页查询 asc
                //按年分
                //开始
                //添加进行中记录

//                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");//设置日期格式
//                String nowName = df.format(new Date());
                SimpleDateFormat dbdate = new SimpleDateFormat("yyMMddHHmmss");//设置日期格式
                String dbstr = dbdate.format(new Date());

                newdataName = databaseName + "_year"+dbstr;

                //路径
                filePath = filePre + newdataName + "/";

                recordsheet.setNewdataName(newdataName);
                recordsheet.setStatus(2);
                recordsheet.setTime(new Date());
//                TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                int insert = baseMapper.insert(recordsheet);

//                System.out.println("插入状态：" + insert);
                while (true) {
//                开启事务
                    TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);

                    try {
                        Map<String, List<Long>> TbMap = new HashMap<>();
                        //数据库查询数据
                        List<Long> list = PagData(databaseName, tableName, tableIdName, offset, pageSize);

                        offset += pageSize;
                        //判断是否有数据
                        boolean empty = CollectionUtils.isNotEmpty(list);
                        if (empty) {
                            //分页有值
                                wb = whetherBase(newdataName);
                            if (wb == 0) {
                                //数据库不存在，则先创建数据库
                                baseMapper.createLibrary(newdataName);

                                //随后插入数据库
                                //设置添加数据库的名字和URL
//                                datasource.setDsname(newdataName);
//                                datasource.setUrl(PRE + newdataName + Suffix);
//                                datasourceMapper.insert(datasource);
                            }
                            //数据库存在，就是wb=1
                            //判断目标表是否存在
                            Integer finRes = 0;

                            // 一页一页的处理
                            for (int i = 0; i < list.size(); i++) {
                                //2.判断哪一年的：
                                SimpleDateFormat format = new SimpleDateFormat("yyyy");

                                //得到当前id对应的时间字段 yyyy年份
                                Long id = list.get(i);
                                //得到数据库Date类型时间值

                                Date date = baseMapper.findDate(fieldTime, databaseName, tableName, tableIdName, id);
                                String year = format.format(date);

                                Name = newtableName; // 需要打印中间代理的Name

                                //3.设置目标表名：格式、例：finance_record_tb年
                                newtableName = tableName + "_" + year;


                                //4.判断目标表是否存在
                                wt = baseMapper.whetherTable(newdataName, newtableName);
                                if (wt == 0) {
                                    //表不存在，则先创建表
                                    baseMapper.addTable(newdataName, newtableName, databaseName, tableName);
                                }

                                boolean result = newtableName.equals(Name);
//                                System.out.println("前面是否等于后面："+result);
                                if (result == false && Name != null) {
                                    //如果前面表不等于后面的， 先判断map里有没有这个表key
                                    boolean b = TbMap.containsKey(newtableName);
                                    //如果有
                                    if (b == true) {//添加id到这个表中

                                        TbMap.get(newtableName).add(id);
//                                        System.out.println("有这个key："+newtableName);
                                    } else {
                                        //如果没有，就new一个list放id 存map
//                                        List<Long> tableIdList= new ArrayList<>();
//                                        tableIdList.add(id);
                                        TbMap.put(newtableName, new ArrayList<>());
                                        TbMap.get(newtableName).add(id);
//                                        System.out.println("没有这个key，这里的name"+Name+"，这里的新表名："+newtableName);
                                    }
                                } else {
                                    boolean c = TbMap.containsKey(newtableName);
//                                    System.out.println("是否有"+newtableName+"::"+c);
                                    if (c == false) {
                                        //如果表名相等,直接添加id
                                        TbMap.put(newtableName, new ArrayList<>());
                                        TbMap.get(newtableName).add(id);
                                    } else {
                                        TbMap.get(newtableName).add(id);
                                    }
                                }
                            }
                            for (Map.Entry<String, List<Long>> entry : TbMap.entrySet()) {
                                try {
                                    //5.放数据
                                    //得到id
                                    //假装出错 放数据时
//                                int d = 1/0;
//                                int result = insertData(newdataName, newtableName,databaseName,tableName,tableIdName,id);
//                                    + ", Value = " + entry.getValue()
//                                    System.out.println("Key = " + entry.getKey());
                                    int result = testInsert(newdataName, entry.getKey(), databaseName, tableName, tableIdName, entry.getValue());
//                                    System.out.println("插入+1");
                                    finRes += result;

                                } catch (RuntimeException e) {
                                    if (e instanceof DuplicateKeyException) {
                                        System.out.println("该接口数据主键冲突" + e.getMessage());
                                        //                            ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    dataSourceTransactionManager.rollback(transactionStatus);
                                        //这页的第一个数据主键已存在，则跳过当前页循环，进行下一页数据插入
                                        break;
                                    } else {
                                        dataSourceTransactionManager.rollback(transactionStatus);
                                        System.out.println("其他错误" + e.getMessage());
                                        //异常错误，失败记录
                                        recordsheet.setStatus(0);
                                        recordsheet.setEndtime(new Date());
                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                                        recordsheet.setSpendtime(spendTime);
                                        //插入失败的记录
                                        baseMapper.updateById(recordsheet);
                                        //                            ws发送消息
                                        JqWebSocket.sendMessage();
                                        return "异常错误";
                                    }
                                }
                            }

//                            ws发送消息
                            JqWebSocket.sendMessage();

                            System.out.println("list==" + empty);
                        } else {
                            //做最后一个表的备份
                            //最后分完做备份
                            List<String> TabNamels = baseMapper.ShowAllTB(newdataName);

                            for (int i = 0; i < TabNamels.size(); i++) {
                                mysqlFileName = TabNamels.get(i) + ".sql";
                                baseMapper.rename(tableName,TabNamels.get(i),newdataName);

                                mysqlBackupsService.mysqlBackups(Ip,filePath, newdataName, tableName, mysqlFileName, userName, passWord);
                                //做删除操作
                                baseMapper.delTB(newdataName,tableName);
                                System.out.println("备份完毕,删除一个已备份的表");
                            }

                            //判断数据库中是否还有表，没有，则删除库，有则不做处理
                            List<String> allTb = baseMapper.ShowAllTB(newdataName);
                            if (allTb.isEmpty()){
                                //判断数据库是否有表，如果为空，就删除该数据库
                                baseMapper.delDataBase(newdataName);
                            }

                            //集合里没有数据了,到最后了，状态为：1.成功
                            //分页无值
                            //0.不删除，1.删除
//                            if (sure == 0) {
//                                //不删除
//                                //添加成功记录
//                                dataSourceTransactionManager.rollback(transactionStatus);
//                                //结束，更改记录数据
//                                recordsheet.setStatus(1);
//                                recordsheet.setEndtime(new Date());
//                                String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                recordsheet.setSpendtime(spendTime);
//                                baseMapper.updateById(recordsheet);
////                                ws发送消息
//                                JqWebSocket.sendMessage();
//                                return "成功，不删除原始数据";
//                            } else {
//                                //删除
//                                try {
//                                    Boolean aBoolean = delSure(databaseName, tableName, tableIdName);
//                                    if (aBoolean == false) {
//                                        dataSourceTransactionManager.rollback(transactionStatus);
//                                        //删除数据失败，失败记录
//                                        recordsheet.setStatus(0);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//
//                                        recordsheet.setSpendtime(spendTime);
//                                        baseMapper.updateById(recordsheet);
//                                        //                                ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "删除失败";
//                                    }
//                                    recordsheet.setStatus(1);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                    recordsheet.setSpendtime(spendTime);
//                                    baseMapper.updateById(recordsheet);
//                                    dataSourceTransactionManager.commit(transactionStatus);
//                                    //  ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    return "成功添加表并删除了原始数据";
//                                } catch (Exception n) {
//                                    System.out.println("n的报错" + n.getMessage());
//                                    dataSourceTransactionManager.rollback(transactionStatus);
//                                    //异常错误，失败记录
//                                    recordsheet.setStatus(0);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//
//                                    recordsheet.setSpendtime(spendTime);
//                                    //插入失败的记录
//                                    baseMapper.updateById(recordsheet);
//                                    //                                ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    return "原始数据删除失败，异常错误";
//                                }
//                            }
                        }
                        //提交事务
                        dataSourceTransactionManager.commit(transactionStatus);

                    } catch (Exception e1) {
                        dataSourceTransactionManager.rollback(transactionStatus);
                        //异常错误，失败记录
                        recordsheet.setStatus(0);
                        recordsheet.setEndtime(new Date());
                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                        recordsheet.setSpendtime(spendTime);
                        //插入失败的记录
                        baseMapper.updateById(recordsheet);
                        //失败后，判断数据库是否存在，存在则插入数据库，不存在则不插入
//                        if (wb == 0) {
//                            datasourceMapper.insert(datasource);
//                        }
                        //                                ws发送消息
                        JqWebSocket.sendMessage();
                        System.out.println("e1的报错" + e1.getMessage());
                        return "添加表失败，请检查填写是否错误";
                    }
                }
            } else if (type == 1) {
                //按月分
                //开始
                //添加进行中记录

//                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");//设置日期格式
//                String nowName = df.format(new Date());
                SimpleDateFormat dbdate = new SimpleDateFormat("yyMMddHHmmss");//设置日期格式
                String dbstr = dbdate.format(new Date());

                newdataName = databaseName + "_months"+dbstr;
                //路径
                filePath = filePre + newdataName + "/";



                recordsheet.setNewdataName(newdataName);
                recordsheet.setStatus(2);
                recordsheet.setTime(new Date());
//                TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                int insert = baseMapper.insert(recordsheet);
//                System.out.println("插入状态：" + insert);
                while (true) {
//                开启事务
                    TransactionStatus MonthsStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                    try {

                        Map<String, List<Long>> TbMap = new HashMap<>();
                        //数据库查询数据
                        List<Long> list = PagData(databaseName, tableName, tableIdName, offset, pageSize);

                        offset += pageSize;
                        //判断是否有数据
                        boolean empty = CollectionUtils.isNotEmpty(list);
                        if (empty) {
                            //分页有值
                            wb = whetherBase(newdataName);

                            if (wb == 0) {
                                //数据库不存在，则先创建数据库
                                baseMapper.createLibrary(newdataName);
                                //随后插入数据库
                                //设置添加数据库的名字和URL
//                                datasource.setDsname(newdataName);
//                                datasource.setUrl(PRE + newdataName + Suffix);
//                                datasourceMapper.insert(datasource);
                            }
                            //数据库存在，就是wb=1
                            //判断目标表是否存在
                            Integer finRes = 0;
                            for (int i = 0; i < list.size(); i++) {

                                //2.判断哪一年的：
                                SimpleDateFormat format = new SimpleDateFormat("yyyy_MM");
                                //得到当前id对应的时间字段 yyyy_MM
                                Long id = list.get(i);
                                //得到数据库Date类型时间值

                                Date date = baseMapper.findDate(fieldTime, databaseName, tableName, tableIdName, id);
                                String year_month = format.format(date);
                                Name = newtableName; // 需要打印中间代理的Name

                                //3.设置目标表名：格式、例：finance_record_tb年月
                                newtableName = tableName + "_" + year_month;
                                //4.判断目标表是否存在
                                wt = baseMapper.whetherTable(newdataName, newtableName);
                                if (wt == 0) {
                                    //表不存在，则先创建表
                                    baseMapper.addTable(newdataName, newtableName, databaseName, tableName);
                                }
                                boolean result = newtableName.equals(Name);
                                if (result == false && Name != null) {
                                    //如果前面表不等于后面的， 先判断map里有没有这个表key
                                    boolean b = TbMap.containsKey(newtableName);
                                    //如果有
                                    if (b == true) {//添加id到这个表中
                                        TbMap.get(newtableName).add(id);
//                                        System.out.println("有这个key：" + newtableName);
                                    } else {
                                        //如果没有，就new一个list放id 存map
//                                        List<Long> tableIdList= new ArrayList<>();
//                                        tableIdList.add(id);
                                        TbMap.put(newtableName, new ArrayList<>());
                                        TbMap.get(newtableName).add(id);
//                                        System.out.println("没有这个key，这里的name" + Name + "，这里的新表名：" + newtableName);
                                    }
                                } else {
                                    boolean c = TbMap.containsKey(newtableName);
//                                    System.out.println("是否有"+newtableName+"::"+c);
                                    if (c == false) {
                                        //如果表名相等,直接添加id
                                        TbMap.put(newtableName, new ArrayList<>());
                                        TbMap.get(newtableName).add(id);
                                    } else {
                                        TbMap.get(newtableName).add(id);
                                    }
                                }
                            }
                            for (Map.Entry<String, List<Long>> entry : TbMap.entrySet()) {
                                try {
                                    //5.放数据
                                    //得到id
                                    //假装出错 放数据时
//                                int d = 1/0;
//                                    int result = baseMapper.insertData(newdataName, newtableName,databaseName,tableName,tableIdName,id);
//                                    finRes += result ;
//                                    System.out.println("Key = " + entry.getKey());
                                    int result = testInsert(newdataName, entry.getKey(), databaseName, tableName, tableIdName, entry.getValue());
//                                    System.out.println("插入+1");
                                    finRes += result;
                                } catch (RuntimeException e) {
                                    if (e instanceof DuplicateKeyException) {
                                        System.out.println("该接口数据主键冲突" + e.getMessage());
//                                    dataSourceTransactionManager.rollback(transactionStatus);
                                        //这页的第一个数据主键已存在，则跳过当前页循环，进行下一页数据插入
                                        break;
                                    } else {
                                        dataSourceTransactionManager.rollback(MonthsStatus);
                                        System.out.println("其他错误" + e.getMessage());
                                        //异常错误，失败记录
                                        recordsheet.setStatus(0);
                                        recordsheet.setEndtime(new Date());
                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                                        recordsheet.setSpendtime(spendTime);
                                        //插入失败的记录
                                        baseMapper.updateById(recordsheet);
                                        //                            ws发送消息
                                        JqWebSocket.sendMessage();
                                        return "异常错误";
                                    }
                                }
                            }
                            //                            ws发送消息
                            JqWebSocket.sendMessage();

//                            System.out.println("最终成功数：" + finRes);
                            System.out.println("list==" + empty);
                        } else {
                            //做最后一个表的备份
                            //最后分完做备份
                            List<String> TabNamels = baseMapper.ShowAllTB(newdataName);

                            for (int i = 0; i < TabNamels.size(); i++) {
                                mysqlFileName = TabNamels.get(i) + ".sql";
                                baseMapper.rename(tableName,TabNamels.get(i),newdataName);

                                mysqlBackupsService.mysqlBackups(Ip,filePath, newdataName, tableName, mysqlFileName, userName, passWord);
                                //做删除操作
                                baseMapper.delTB(newdataName,tableName);
                                System.out.println("备份完毕,删除一个已备份的表");
                            }

                            //判断数据库中是否还有表，没有，则删除库，有则不做处理
                            List<String> allTb = baseMapper.ShowAllTB(newdataName);
                            if (allTb.isEmpty()){
                                //判断数据库是否有表，如果为空，就删除该数据库
                                baseMapper.delDataBase(newdataName);
                            }

                            //集合里没有数据了,到最后了，状态为：1.成功
                            //分页无值
                            //0.不删除，1.删除
//                            if (sure == 0) {
//                                //不删除
//                                //添加成功记录
//                                dataSourceTransactionManager.rollback(MonthsStatus);
//                                recordsheet.setStatus(1);
//                                recordsheet.setEndtime(new Date());
//                                String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//
//                                recordsheet.setSpendtime(spendTime);
//                                baseMapper.updateById(recordsheet);
//                                //                                ws发送消息
//                                JqWebSocket.sendMessage();
//                                return "成功，不删除原始数据";
//                            } else {
//                                //删除
//                                try {
//                                    Boolean aBoolean = delSure(databaseName, tableName, tableIdName);
//                                    if (aBoolean == false) {
//                                        dataSourceTransactionManager.rollback(MonthsStatus);
//                                        //删除数据失败，失败记录
//                                        recordsheet.setStatus(0);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                        recordsheet.setSpendtime(spendTime);
//                                        baseMapper.updateById(recordsheet);
//                                        //                                ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "删除失败";
//                                    }
//                                    recordsheet.setStatus(1);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                    recordsheet.setSpendtime(spendTime);
//                                    baseMapper.updateById(recordsheet);
//                                    dataSourceTransactionManager.commit(MonthsStatus);
//                                    //                                ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    return "成功添加表并删除了原始数据";
//                                } catch (Exception n) {
//                                    System.out.println("n的报错" + n.getMessage());
//                                    dataSourceTransactionManager.rollback(MonthsStatus);
//                                    //异常错误，失败记录
//                                    recordsheet.setStatus(0);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                    recordsheet.setSpendtime(spendTime);
//                                    //插入失败的记录
//                                    baseMapper.updateById(recordsheet);
//                                    //                                ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    return "原始数据删除失败，异常错误";
//                                }
//                            }
                        }
                        //提交事务
                        dataSourceTransactionManager.commit(MonthsStatus);
                    } catch (Exception e1) {
                        dataSourceTransactionManager.rollback(MonthsStatus);
                        //异常错误，失败记录
                        recordsheet.setStatus(0);
                        recordsheet.setEndtime(new Date());
                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                        recordsheet.setSpendtime(spendTime);
                        //插入失败的记录
                        baseMapper.updateById(recordsheet);
                        //失败后，判断数据库是否存在，存在则插入数据库，不存在则不插入
//                        if (wb == 0) {
//                            datasourceMapper.insert(datasource);
//                        }
                        //                                ws发送消息
                        JqWebSocket.sendMessage();
                        System.out.println("e1的报错" + e1.getMessage());
                        return "添加表失败，请检查填写是否错误";
                    }
                }
            } else if (type == 2) {
                //按周分
                //开始
                //添加进行中记录

//                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");//设置日期格式
//                String nowName = df.format(new Date());
                SimpleDateFormat dbdate = new SimpleDateFormat("yyMMddHHmmss");//设置日期格式
                String dbstr = dbdate.format(new Date());

                newdataName = databaseName + "_weeks"+dbstr;
                //路径
                filePath = filePre + newdataName + "/";

                recordsheet.setNewdataName(newdataName);
                recordsheet.setStatus(2);
                recordsheet.setTime(new Date());
//                TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                int insert = baseMapper.insert(recordsheet);
//                System.out.println("插入状态：" + insert);
                while (true) {
//                开启事务
                    TransactionStatus WeeksStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                    try {
                        Map<String, List<Long>> TbMap = new HashMap<>();
                        //数据库查询数据
                        List<Long> list = PagData(databaseName, tableName, tableIdName, offset, pageSize);

                        offset += pageSize;
                        //判断是否有数据
                        boolean empty = CollectionUtils.isNotEmpty(list);
                        if (empty) {
                            //分页有值
                            wb = whetherBase(newdataName);

                            if (wb == 0) {
                                //数据库不存在，则先创建数据库
                                baseMapper.createLibrary(newdataName);
                                //随后插入数据库
                                //设置添加数据库的名字和URL
//                                datasource.setDsname(newdataName);
//                                datasource.setUrl(PRE + newdataName + Suffix);
//                                datasourceMapper.insert(datasource);
                            }
                            //数据库存在，就是wb=1
                            //判断目标表是否存在
                            Integer finRes = 0;

                            for (int i = 0; i < list.size(); i++) {

//                                //2.判断哪一年的：
//                                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                                //得到当前id对应的时间字段 yyyy年份
                                Long id = list.get(i);
                                //得到数据库Date类型时间值
                                Date date = baseMapper.findDate(fieldTime, databaseName, tableName, tableIdName, id);
                                Calendar calendar = Calendar.getInstance();
                                calendar.setFirstDayOfWeek(Calendar.MONDAY);
                                calendar.setTime(date);
                                int week = calendar.get(Calendar.WEEK_OF_YEAR);

                                Name = newtableName; // 需要打印中间代理的Name

                                //3.设置目标表名：格式、例：finance_record_tb week_周
                                newtableName = tableName + "_" + "week_" + week;
//                                System.out.println("周表名："+newtableName);

                                //4.判断目标表是否存在
                                wt = baseMapper.whetherTable(newdataName, newtableName);
                                if (wt == 0) {
                                    //表不存在，则先创建表
                                    baseMapper.addTable(newdataName, newtableName, databaseName, tableName);
                                }

                                boolean result = newtableName.equals(Name);
//                                System.out.println("前面是否等于后面："+result);
                                if (result == false && Name != null) {
                                    //如果前面表不等于后面的， 先判断map里有没有这个表key
                                    boolean b = TbMap.containsKey(newtableName);
                                    //如果有
                                    if (b == true) {//添加id到这个表中

                                        TbMap.get(newtableName).add(id);
//                                        System.out.println("有这个key：" + newtableName);
                                    } else {
                                        //如果没有，就new一个list放id 存map
//                                        List<Long> tableIdList= new ArrayList<>();
//                                        tableIdList.add(id);
                                        TbMap.put(newtableName, new ArrayList<>());
                                        TbMap.get(newtableName).add(id);
//                                        System.out.println("没有这个key，这里的name" + Name + "，这里的新表名：" + newtableName);
                                    }
                                } else {
                                    boolean c = TbMap.containsKey(newtableName);
//                                    System.out.println("是否有"+newtableName+"::"+c);
                                    if (c == false) {
                                        //如果表名相等,直接添加id
                                        TbMap.put(newtableName, new ArrayList<>());
                                        TbMap.get(newtableName).add(id);
                                    } else {
                                        TbMap.get(newtableName).add(id);
                                    }
                                }
                            }
                            for (Map.Entry<String, List<Long>> entry : TbMap.entrySet()) {
                                try {
                                    //5.放数据
                                    //得到id
                                    //假装出错 放数据时
//                                int d = 1/0;
//                                    int result = baseMapper.insertData(newdataName, newtableName,databaseName,tableName,tableIdName,id);
//                                    finRes += result ;
//                                    System.out.println("Key = " + entry.getKey());
                                    int result = testInsert(newdataName, entry.getKey(), databaseName, tableName, tableIdName, entry.getValue());
//                                    System.out.println("插入+1");
                                    finRes += result;
                                } catch (RuntimeException e) {
                                    if (e instanceof DuplicateKeyException) {
                                        System.out.println("该接口数据主键冲突" + e.getMessage());
//                                    dataSourceTransactionManager.rollback(transactionStatus);
                                        //这页的第一个数据主键已存在，则跳过当前页循环，进行下一页数据插入
                                        break;
                                    } else {
                                        dataSourceTransactionManager.rollback(WeeksStatus);
                                        System.out.println("其他错误" + e.getMessage());
                                        //异常错误，失败记录
                                        recordsheet.setStatus(0);
                                        recordsheet.setEndtime(new Date());
                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                                        recordsheet.setSpendtime(spendTime);
                                        //插入失败的记录
                                        baseMapper.updateById(recordsheet);
                                        //                            ws发送消息
                                        JqWebSocket.sendMessage();
                                        return "异常错误";
                                    }
                                }
                            }
//                            ws发送消息
                            JqWebSocket.sendMessage();

                            System.out.println("list==" + empty);
                        } else {
                            //做最后一个表的备份
//                            mysqlFileName = Name + ".sql";
//                            mysqlBackupsService.mysqlBackups(filePath,newdataName,Name,mysqlFileName,userName,passWord);
                            //最后分完做备份
                            List<String> TabNamels = baseMapper.ShowAllTB(newdataName);

                            for (int i = 0; i < TabNamels.size(); i++) {
                                mysqlFileName = TabNamels.get(i) + ".sql";
                                baseMapper.rename(tableName,TabNamels.get(i),newdataName);

                                mysqlBackupsService.mysqlBackups(Ip,filePath, newdataName, tableName, mysqlFileName, userName, passWord);
                                //做删除操作
                                baseMapper.delTB(newdataName,tableName);
                                System.out.println("备份完毕,删除一个已备份的表");
                            }

                            //判断数据库中是否还有表，没有，则删除库，有则不做处理
                            List<String> allTb = baseMapper.ShowAllTB(newdataName);
                            if (allTb.isEmpty()){
                                //判断数据库是否有表，如果为空，就删除该数据库
                                baseMapper.delDataBase(newdataName);
                            }

                            //集合里没有数据了,到最后了，状态为：1.成功
                            //分页无值
                            //0.不删除，1.删除
//                            if (sure == 0) {
//                                //不删除
//                                //添加成功记录
//                                dataSourceTransactionManager.rollback(WeeksStatus);
//                                recordsheet.setStatus(1);
//                                recordsheet.setEndtime(new Date());
//                                String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                recordsheet.setSpendtime(spendTime);
//                                baseMapper.updateById(recordsheet);
//                                //                                ws发送消息
//                                JqWebSocket.sendMessage();
//                                return "成功，不删除原始数据";
//                            } else {
//                                //删除
//                                try {
//                                    Boolean aBoolean = delSure(databaseName, tableName, tableIdName);
//                                    if (aBoolean == false) {
//                                        dataSourceTransactionManager.rollback(WeeksStatus);
//                                        //删除数据失败，失败记录
//                                        recordsheet.setStatus(0);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                        recordsheet.setSpendtime(spendTime);
//                                        baseMapper.updateById(recordsheet);
//                                        //                                ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "删除失败";
//                                    }
//                                    recordsheet.setStatus(1);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                    recordsheet.setSpendtime(spendTime);
//                                    baseMapper.updateById(recordsheet);
//                                    dataSourceTransactionManager.commit(WeeksStatus);
//                                    //                                ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    return "成功添加表并删除了原始数据";
//
//                                } catch (Exception n) {
//                                    System.out.println("n的报错" + n.getMessage());
//                                    dataSourceTransactionManager.rollback(WeeksStatus);
//                                    //异常错误，失败记录
//                                    recordsheet.setStatus(0);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                    recordsheet.setSpendtime(spendTime);
//                                    //插入失败的记录
//                                    baseMapper.updateById(recordsheet);
//                                    //                                ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    return "原始数据删除失败，异常错误";
//                                }
//                            }
                        }
                        //提交事务
                        dataSourceTransactionManager.commit(WeeksStatus);
                    } catch (Exception e1) {
                        dataSourceTransactionManager.rollback(WeeksStatus);
                        //异常错误，失败记录
                        recordsheet.setStatus(0);
                        recordsheet.setEndtime(new Date());
                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                        recordsheet.setSpendtime(spendTime);
                        //插入失败的记录
                        baseMapper.updateById(recordsheet);
                        //失败后，判断数据库是否存在，存在则插入数据库，不存在则不插入
//                        if (wb == 0) {
//                            datasourceMapper.insert(datasource);
//                        }
                        //                                ws发送消息
                        JqWebSocket.sendMessage();
                        System.out.println("e1的报错" + e1.getMessage());
                        return "添加表失败，请检查填写是否错误";
                    }
                }
            } else if (type == 3) {
                //按日分
                //按年分
                //开始
                //添加进行中记录
//                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");//设置日期格式
//                String nowName = df.format(new Date());
                SimpleDateFormat dbdate = new SimpleDateFormat("yyMMddHHmmss");//设置日期格式
                String dbstr = dbdate.format(new Date());

                newdataName = databaseName + "_day"+dbstr;
                //路径
                filePath = filePre + newdataName + "/";

                recordsheet.setNewdataName(newdataName);
                recordsheet.setStatus(2);
                recordsheet.setTime(new Date());
//                TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                int insert = baseMapper.insert(recordsheet);
//                System.out.println("插入状态：" + insert);
                while (true) {
//                开启事务
                    TransactionStatus DaysStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
                    try {
                        Map<String, List<Long>> TbMap = new HashMap<>();
                        //数据库查询数据
                        List<Long> list = PagData(databaseName, tableName, tableIdName, offset, pageSize);

                        offset += pageSize;
                        //判断是否有数据
                        boolean empty = CollectionUtils.isNotEmpty(list);
                        if (empty) {
                            //分页有值
                            wb = whetherBase(newdataName);

                            if (wb == 0) {
                                //数据库不存在，则先创建数据库
                                baseMapper.createLibrary(newdataName);
                                //随后插入数据库
                                //设置添加数据库的名字和URL
//                                datasource.setDsname(newdataName);
//                                datasource.setUrl(PRE + newdataName + Suffix);
//                                datasourceMapper.insert(datasource);
                            }
                            //数据库存在，就是wb=1
                            //判断目标表是否存在
                            Integer finRes = 0;


                            for (int i = 0; i < list.size(); i++) {
                                //2.判断哪一年的：
                                SimpleDateFormat format = new SimpleDateFormat("yyyy_MM_dd");
                                //得到当前id对应的时间字段 yyyy年份
                                Long id = list.get(i);
                                //得到数据库Date类型时间值

                                Date date = baseMapper.findDate(fieldTime, databaseName, tableName, tableIdName, id);
                                String day = format.format(date);
                                Name = newtableName; // 需要打印中间代理的Name

                                //3.设置目标表名：格式、例：finance_record_tb_天
                                newtableName = tableName + "_" + day;
                                //4.判断目标表是否存在
                                wt = baseMapper.whetherTable(newdataName, newtableName);
                                if (wt == 0) {
                                    //表不存在，则先创建表
                                    baseMapper.addTable(newdataName, newtableName, databaseName, tableName);
                                }

                                boolean result = newtableName.equals(Name);
//                                System.out.println("前面是否等于后面："+result);
                                if (result == false && Name != null) {
                                    //如果前面表不等于后面的， 先判断map里有没有这个表key
                                    boolean b = TbMap.containsKey(newtableName);
                                    //如果有
                                    if (b == true) {//添加id到这个表中

                                        TbMap.get(newtableName).add(id);
//                                        System.out.println("有这个key：" + newtableName);
                                    } else {
                                        //如果没有，就new一个list放id 存map
//                                        List<Long> tableIdList= new ArrayList<>();
//                                        tableIdList.add(id);
                                        TbMap.put(newtableName, new ArrayList<>());
                                        TbMap.get(newtableName).add(id);
//                                        System.out.println("没有这个key，这里的name" + Name + "，这里的新表名：" + newtableName);
                                    }
                                } else {
                                    boolean c = TbMap.containsKey(newtableName);
//                                    System.out.println("是否有"+newtableName+"::"+c);
                                    if (c == false) {
                                        //如果表名相等,直接添加id
                                        TbMap.put(newtableName, new ArrayList<>());
                                        TbMap.get(newtableName).add(id);
                                    } else {
                                        TbMap.get(newtableName).add(id);
                                    }
                                }
                            }
                            for (Map.Entry<String, List<Long>> entry : TbMap.entrySet()) {
                                try {
                                    //5.放数据
                                    //得到id
                                    //假装出错 放数据时
//                                int d = 1/0;
//                                    int result = baseMapper.insertData(newdataName, newtableName,databaseName,tableName,tableIdName,id);
//                                    finRes += result ;
//                                    System.out.println("Key = " + entry.getKey());
                                    int result = testInsert(newdataName, entry.getKey(), databaseName, tableName, tableIdName, entry.getValue());
//                                    System.out.println("插入+1");
                                    finRes += result;
                                } catch (RuntimeException e) {
                                    if (e instanceof DuplicateKeyException) {
                                        System.out.println("该接口数据主键冲突" + e.getMessage());
//                                    dataSourceTransactionManager.rollback(transactionStatus);
                                        //这页的第一个数据主键已存在，则跳过当前页循环，进行下一页数据插入
                                        break;
                                    } else {
                                        dataSourceTransactionManager.rollback(DaysStatus);
                                        System.out.println("其他错误" + e.getMessage());
                                        //异常错误，失败记录
                                        recordsheet.setStatus(0);
                                        recordsheet.setEndtime(new Date());
                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                                        recordsheet.setSpendtime(spendTime);
                                        //插入失败的记录
                                        baseMapper.updateById(recordsheet);
                                        //                            ws发送消息
                                        JqWebSocket.sendMessage();
                                        return "异常错误";
                                    }
                                }
                            }
                            //                            ws发送消息
                            JqWebSocket.sendMessage();
//                            System.out.println("最终成功数：" + finRes);
                            System.out.println("list==" + empty);
                        } else {
                            //做最后一个表的备份
//                            mysqlFileName = Name + ".sql";
//                            mysqlBackupsService.mysqlBackups(filePath,newdataName,Name,mysqlFileName,userName,passWord);
                            //最后分完做备份
                            List<String> TabNamels = baseMapper.ShowAllTB(newdataName);

                            for (int i = 0; i < TabNamels.size(); i++) {
                                mysqlFileName = TabNamels.get(i) + ".sql";
                                baseMapper.rename(tableName,TabNamels.get(i),newdataName);

                                mysqlBackupsService.mysqlBackups(Ip,filePath, newdataName, tableName, mysqlFileName, userName, passWord);
                                //做删除操作
                                baseMapper.delTB(newdataName,tableName);
                                System.out.println("备份完毕,删除一个已备份的表");
                            }

                            //判断数据库中是否还有表，没有，则删除库，有则不做处理
                            List<String> allTb = baseMapper.ShowAllTB(newdataName);
                            if (allTb.isEmpty()){
                                //判断数据库是否有表，如果为空，就删除该数据库
                                baseMapper.delDataBase(newdataName);
                            }

                            //集合里没有数据了,到最后了，状态为：1.成功
                            //分页无值
                            //0.不删除，1.删除
//                            if (sure == 0) {
//                                //不删除
//                                //添加成功记录
//                                dataSourceTransactionManager.rollback(DaysStatus);
//                                recordsheet.setStatus(1);
//                                recordsheet.setEndtime(new Date());
//                                String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                recordsheet.setSpendtime(spendTime);
//                                baseMapper.updateById(recordsheet);
//                                return "成功，不删除原始数据";
//                            } else {
//                                //删除
//                                try {
//                                    Boolean aBoolean = delSure(databaseName, tableName, tableIdName);
//                                    if (aBoolean == false) {
//                                        dataSourceTransactionManager.rollback(DaysStatus);
//                                        //删除数据失败，失败记录
//                                        recordsheet.setStatus(0);
//                                        recordsheet.setEndtime(new Date());
//                                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                        recordsheet.setSpendtime(spendTime);
//                                        baseMapper.updateById(recordsheet);
//                                        //                                ws发送消息
//                                        JqWebSocket.sendMessage();
//                                        return "删除失败";
//                                    }
//                                    recordsheet.setStatus(1);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                    recordsheet.setSpendtime(spendTime);
//                                    baseMapper.updateById(recordsheet);
//                                    dataSourceTransactionManager.commit(DaysStatus);
//                                    //                                ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    return "成功添加表并删除了原始数据";
//                                } catch (Exception n) {
//                                    System.out.println("n的报错" + n.getMessage());
//                                    dataSourceTransactionManager.rollback(DaysStatus);
//                                    //异常错误，失败记录
//                                    recordsheet.setStatus(0);
//                                    recordsheet.setEndtime(new Date());
//                                    String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
//                                    recordsheet.setSpendtime(spendTime);
//                                    //插入失败的记录
//                                    baseMapper.updateById(recordsheet);
//
//                                    //                                ws发送消息
//                                    JqWebSocket.sendMessage();
//                                    return "原始数据删除失败，异常错误";
//
//                                }
//                            }
                        }
                        //提交事务
                        dataSourceTransactionManager.commit(DaysStatus);
                    } catch (Exception e1) {
                        dataSourceTransactionManager.rollback(DaysStatus);
                        //异常错误，失败记录
                        recordsheet.setStatus(0);
                        recordsheet.setEndtime(new Date());
                        String spendTime = TakeTimeUtil.dateDiff(recordsheet.getTime(), recordsheet.getEndtime());
                        recordsheet.setSpendtime(spendTime);
                        //插入失败的记录
                        baseMapper.updateById(recordsheet);
                        //失败后，判断数据库是否存在，存在则插入数据库，不存在则不插入
//                        if (wb == 0) {
//                            datasourceMapper.insert(datasource);
//                        }
                        //                                ws发送消息
                        JqWebSocket.sendMessage();
                        System.out.println("e1的报错" + e1.getMessage());
                        return "添加表失败，请检查填写是否错误";
                    }
                }
            }
        }
        return "OK";
    }

    /**
     * 分页实现类
     *
     * @param databaseName 目标数据库
     * @param tableName 目标表
     * @param tableIdName 目标主键
     * @param offset 偏移量
     * @param pageSize 每页多少数据
     * @return
     */
    @Override
    public List<Long> PagData(String databaseName,String tableName, String tableIdName, Integer offset, Integer pageSize) {
        List<Long> list = baseMapper.PagData(databaseName,tableName, tableIdName, offset, pageSize);
        return list;
    }

//    @Override
//    public List<Long> LastIdPagData(String databaseName, String tableName, Long lastId, String tableIdName, Integer offset, Integer pageSize) {
//        List<Long> longs = baseMapper.LastIdPagData(databaseName, tableName, lastId, tableIdName, offset, pageSize);
//        return longs;
//    }

    //确认数据库是否存在
    @Override
    public int whetherBase(String newdataName) {
        int i = baseMapper.whetherBase(newdataName);
//        System.out.println("输出数据库是否存在："+i);
        return i;
    }

    //删除全部数据实现类
//    @SneakyThrows
//    @Override
//    public Boolean delSure(String databaseName,String tableName,String tableIdName) {
//        int count = baseMapper.count(databaseName, tableName);
//        int ceil = (int) Math.ceil(count / 1000.0);
//        System.out.println("循环次数："+ceil);
//        for (int i = 1; i <= ceil; i++) {
////            System.out.println("开始删除执行");
//            Boolean b = baseMapper.delSure(databaseName, tableName, tableIdName);
//            System.out.println("删除："+b+"，第"+i+"次");
//            //                                ws发送消息
//            JqWebSocket.sendMessage();
//            if (b==false){
//                return false;
//            }
//        }
//        return true;
//    }


    //自定义的删除部分数据
//    @Override
//    public int delCustomSure(String databaseName,String tableName,String tableIdName,List<Long> allIdList) {
//        List<Long> newList = new ArrayList<>();
//        //总条数
//        int total = allIdList.size();
//        System.out.println("总数："+total);
//        //第一页的最后一个的下标
//        int lastIndex = 1000;
//
//        //循环次数
//        int nub = (int) Math.ceil(total / 1000);
//        System.out.println("循环次数:"+nub);
//
//        //取余
//        int Remainder = total % lastIndex;
//
//        //下标
//        int index = 0;
//
//        //删除的总数
//        int delSum=0;
//
//        if (Remainder == 0){
//            //如果没有余数,循环次数不变
//            for (int i = 0; i < nub; i++) {
//                newList = allIdList.subList(index,lastIndex);
//                //删除工作
//                System.out.println("第"+i+"次"+"，index为："+index+"，lastindex为："+lastIndex);
//                int delNub = baseMapper.testDel(databaseName, tableName, tableIdName, newList);
//                index = lastIndex;
//                lastIndex = lastIndex+1000;
//
//                System.out.println("无余数，已删除："+delNub);
//                delSum= delNub+delSum;
//            }
//            System.out.println("删除总数："+delSum);
//            return delSum;
//
//        }else {
//            //有余数
//            int nubOne = nub+1;
//            for (int i = 0; i < nubOne; i++) {
//                if (i==nub){
//                    System.out.println("i等于nub");
//                    index = lastIndex-1000;
//                    lastIndex = total;
//                }
//                newList = allIdList.subList(index,lastIndex);
//
//                System.out.println("第"+i+"次"+"，index为："+index+"，lastindex为："+lastIndex);
//                //删除工作
//                int delNub = baseMapper.testDel(databaseName, tableName, tableIdName, newList);
//
//                index = lastIndex;
//                lastIndex = lastIndex+1000;
//
//                System.out.println("有余数，已删除"+delNub);
//                delSum= delNub+delSum;
//            }
//            return delSum;
//        }
//    }

    //显示记录数据
    @Override
    public List<Recordsheet> showRecord() {
        List<Recordsheet> list = baseMapper.showRecord();
        return list;
    }

//    @Override
//    public int delRecord(Long id) {
//        int i = baseMapper.delRecord(id);
//        return i;
//    }

    @Override
//    @Async("asyncServiceExecutor")
    public int insertData(String newdataName, String newtableName, String databaseName, String tableName, String tableIdName, Long Id) {
        int i = baseMapper.insertData(newdataName, newtableName, databaseName, tableName, tableIdName, Id);
        return 0;
    }

    @Override
    public int testInsert(String newdataName,String newtableName,String databaseName, String tableName, String tableIdName, List<Long> ids) {
        int i = baseMapper.testInsert(newdataName,newtableName,databaseName, tableName, tableIdName, ids);
        return i;
    }


//    @Override
//    public List<String> ShowAllTB(String newdataName) {
//        List<String> list = baseMapper.ShowAllTB(newdataName);
//        if (list.isEmpty()){
//            //判断数据库是否有表，如果为空，就删除该数据库
//            System.out.println("打印的到的list："+list);
//        }else {
//            //如果不为空，就不做处理
//            System.out.println("不为空");
//        }
//        return null;
//    }


}
